[go: up one dir, main page]

0% found this document useful (0 votes)
40 views3 pages

Control Your PC From Anywhere

This document discusses using Python for socket programming to control a PC remotely. It provides an overview of the key functions used like socket(), bind(), listen(), accept(), connect(), recv(), send(). It then presents the code for a server program and client program that allow sending commands from one PC to another to remotely execute programs. The server program accepts connections, takes a command, sends it to the client and receives confirmation. The client program connects to the server, receives and decodes the command, executes it if it matches the input, and sends confirmation back.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views3 pages

Control Your PC From Anywhere

This document discusses using Python for socket programming to control a PC remotely. It provides an overview of the key functions used like socket(), bind(), listen(), accept(), connect(), recv(), send(). It then presents the code for a server program and client program that allow sending commands from one PC to another to remotely execute programs. The server program accepts connections, takes a command, sends it to the client and receives confirmation. The client program connects to the server, receives and decodes the command, executes it if it matches the input, and sends confirmation back.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

pc control from any where...

Introduction

Hello everyone, in this tutorial we are going to learn how to control the PC from
anywhere using Python. For this purpose, we are going to understand the concept of
socket programming. We are using sockets to establish communication between two PCs
by connecting them over the network.

We will understand this in brief as we move forward with our code. A detailed
explanation of each line of code and comments are provided to understand the
program properly.

Here we are performing communication between two different PCs. So we need two
different programs which will run on two different terminals. We will name the two
programs serverprog.py and clientprog.py.

We are providing some functions and their description used in this project.

Function Description
///////////////////////////////////////////////////////////////////////////////////
//////////
socket.socket() Create sockets.
socket.bind() This binds hostname and portname to the socket.
socket.listen() This starts the TCP listener.
socket.accept() Accept client connection and waits till the
connection arrives.
socket.connect() Initiates the TCP connection.
s.recv() It receives TCP message
s.send() It sends TCP message
socket.gethostname() It returns hostname

------------/******/*/***/

So Let’s begin with the programs

Import Function----------

import time
import socket
import sys
import os

Here we are importing four different modules time, socket, sys, and os. The time
module allows working with the functionality of time. The socket module is
performing connections between 2 nodes on the network. The sys module is providing
valuable information about the Python interpreter. The os module interacts with the
operating system.

Develop a code to create the connection with client program-------

soc = socket.socket()
host = socket.gethostname()
port = 8080
soc.bind(('', port))

print("waiting for connections...")


soc.listen()
conn, addr = soc.accept()

print(addr, "is connected to server")


command = input(str("Enter Command :"))
conn.send(command.encode())
print("Command has been sent successfully.")
data = conn.recv(1024)

if data:
print("command received and executed successfully.")

-In this code block, we are creating the variable soc of socket type which
creates the socket. The host variable takes the hostname. Initialize the port to
8080. The hostname and port get binds to each other, till the time it waits for a
connection. The soc.listen() functions waits for TCP listener .soc.accept()function
accepts the connection and stores in the variable conn,addr. Once the connection
happens it prints the message “is connected to server”.

-The command variable takes the input and sends the TCP message to the client to
encode. The data variable stores the received message from the client. Once we
receive the message in the data variable it prints the message “command received
and executed successfully”
Develop a code to create the connection with the server program

soc = socket.socket()
host = "127.0.0.1”
port = 8080
soc.connect((host, port))
print("Connected to Server.")
command = soc.recv(1024)
command = command.decode()
if command == "open":
print("Command is :", command)
soc.send("Command received".encode())
os.system('test.bat')

-Here in the client part, we are creating the soc variable. Initializing the host
variable to “127.0.0.1″ and port to 8080. The soc.connect((host, port)) initiates
the TCP connection and binds the socket to the host and port. The command that we
received from the server is stored here in the command variable .command.decode()
function decodes the message. If the decoded message and command the user input is
the same then print the command. The soc.send () function sends the TCP message by
encoding it. Here we are providing the file name in os.system to display the
output.

Complete Code for Control PC from anywhere using Python------

Exceute the program serverprog.py/------


******/

import time
import socket
import sys
import os

# Initialize soc to socket


soc = socket.socket()

# Initialize the host


host = socket.gethostname()

# Initialize the port


port = 8080

# Bind the socket with port and host


soc.bind(('', port))

print("waiting for connections...")

# listening for connections


soc.listen()

# accepting the incoming connections


conn, addr = soc.accept()

print(addr, "is connected to server")

# take command as input


command = input(str("Enter Command :"))

conn.send(command.encode())

print("Command has been sent successfully.")

# receive the confirmation


data = conn.recv(1024)

if data:
print("command received and executed successfully.")

You might also like