How to control PC from anywhere using Python?
Prerequisite – Socket programming in Python
In this solution, we use the concept of Socket Programming for establishing communication between two computers.
Socket Programming in Python
Socket Programming is a way of connecting two systems on a network to communicate with each other. Sockets are the endpoints built for sending and receiving data and it is a combination of IP address and port. We will import the socket module to use Socket Programming in Python. The following are the methods required to build the solution:
Methods in Socket Module:
| Method | Description |
| socket.socket(). | Create sockets. |
| socket.bind() | This method bind hostname and portname to socket. |
| socket.listen() | This method starts the TCP listener. |
| socket.accept() | Accept client connection and wait until the connection arrives. |
| socket.connect() | Initiate TCP connection. |
| socket.close() | Close the socket. |
Other Socket Methods:
| Method | Description |
| s.recv() | It receives TCP message |
| s.send() | It sends TCP message |
| socket.gethostname() | It returns hostname |
So we have to develop two python programs one is master.py (server)and another is slave.py (client), using master.py we can control the system having slave.py program. To control the pc from anywhere using python please the follow the step mentioned below:
Step 1: Create and Execute the “master.py” in one terminal
Python3
import timeimport socketimport sysimport os# Initialize s to sockets = socket.socket()# Initialize the hosthost = socket.gethostname()# Initialize the portport = 8080# Bind the socket with port and hosts.bind(('', port))print("waiting for connections...")# listening for connectionss.listen()# accepting the incoming connectionsconn, addr = s.accept()print(addr, "is connected to server")# take command as inputcommand = input(str("Enter Command :"))conn.send(command.encode())print("Command has been sent successfully.")# receive the confirmationdata = conn.recv(1024)if data: print("command received and executed successfully.") |
Step 2: Create and Execute the “slave.py” is another terminal
Python3
import timeimport socketimport sysimport os# Initialize s to sockets = socket.socket()# Initialize the hosthost = "127.0.0.1"# Initialize the portport = 8080# bind the socket with port and hosts.connect((host, port))print("Connected to Server.")# receive the command from master programcommand = s.recv(1024)command = command.decode()# match the command and execute it on slave systemif command == "open": print("Command is :", command) s.send("Command received".encode()) # you can give batch file as input here os.system('ls') |
Output:
terminal running master.py
terminal running slave.py


