How to control PC from anywhere using Python? Last Updated : 12 Jul, 2021 Comments Improve Suggest changes Like Article Like Report 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: MethodDescription 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: MethodDescriptions.recv()It receives TCP message s.send()It sends TCP messagesocket.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 time import socket import sys import os # Initialize s to socket s = socket.socket() # Initialize the host host = socket.gethostname() # Initialize the port port = 8080 # Bind the socket with port and host s.bind(('', port)) print("waiting for connections...") # listening for connections s.listen() # accepting the incoming connections conn, addr = s.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.") Step 2: Create and Execute the "slave.py" is another terminal Python3 import time import socket import sys import os # Initialize s to socket s = socket.socket() # Initialize the host host = "127.0.0.1" # Initialize the port port = 8080 # bind the socket with port and host s.connect((host, port)) print("Connected to Server.") # receive the command from master program command = s.recv(1024) command = command.decode() # match the command and execute it on slave system if 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.pyterminal running slave.py Comment More infoAdvertise with us Next Article How to control PC from anywhere using Python? A abhijitmahajan772 Follow Improve Article Tags : Python Python-socket Practice Tags : python Similar Reads How to Control Laptop Screen Brightness using Python? To control the brightness of the screen we are using the screen-brightness-control library. The screen-brightness-control library has only one class and few functions. The most useful functions are mentioned below: get_brightness()set_brightness()fade_brightness()list_monitors() Installation: We can 4 min read How to connect WiFi using Python? Seeing a computer without an active internet connection today is next to impossible. The Internet has been of the utmost importance in the 21st Century. There are multiple ways one can connect their machine to the Internet. The first being, the traditional cables, i.e. the Ethernet, and the other be 5 min read Run python script from anywhere in linux In Linux, there is a way to execute python files from anywhere. This can be done by typing several commands in the terminal. Prerequisite: Basic Shell Commands in Linux Basics of python Steps: At first, open the terminal and go to the home directory. To go the home directory type the following comma 2 min read Build a GUI Application to ping the host using Python Prerequisite: Python GUI â Tkinter In this article, we are going to see how to ping the host with a URL or IP using the python ping module in Python. This module provides a simple way to ping in python. And It checks the host is available or not and measures how long the response takes. âBeforeâ sta 2 min read Control Arduino with Python and pyFirmata In this article, we will learn how to link an Arduino to a Python script in order to operate the Arduino. This example of constructing a 4-bit binary up-counter using Python script to control Arduino helps us understand this better. Components Required:An Arduino Board (We will be using Arduino UNO, 4 min read Like