
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.")
Execute the program clientprog.py
import time
import socket
import sys
import os
# Initialize soc to socket
soc = socket.socket()
# Initialize the host
host = "127.0.0.1"
# Initialize the port
port = 8080
# bind the socket with port and host
soc.connect((host, port))
print("Connected to Server.")
# receive the command from server program
command = soc.recv(1024)
command = command.decode()
# match the command and execute it on client system
if command == "open":
print("Command is :", command)
soc.send("Command received".encode())
# #give the file name as input
os.system('test.bat')
Output
After executing the serverprog.py

After executing the clientprog.py

Here we have successfully formed the connection between two nodes on the network.
For more such articles on python keep visiting our website www.copyassignment.com.
Thank you for reading this article.
Also Read:
- Contact Management System Project in Python
- Python SQLite Tutorial
- Student Management System Project in Python
- 20 Python Projects for Resume
- Restaurant management system project in Python
- Employee Management System Project in Python
- Bank Management System Project in Python
- Hospital Management System Project in Python
- ENGG.8.1 .1.3 Assignment 2: Using thermal images to monitor photovoltaic plants
- Attendance Management System Project in Python
- MNIST Handwritten Digit Classification using Deep Learning
- Space Invaders game using Python
- How to make KBC Quiz game in python?
- Control Mouse with hand gestures detection python
- Create a Vehicle Parking Management Project in Python
- Build Your Own Map Flight Tracking Application with Python
- Traffic Signal Violation Detection System using Computer Vision
- Deepfake Detection Project Using Deep-Learning
- Employment Trends Of Private & Government Sector in UAE | Data Analysis
- Pokemon Analysis Project in ML and Data Science using Python
- Garment Factory Analysis Project using Python
- Creating a Pong Game using Python Turtle
- Create a Stopwatch Using Python Tkinter
- Hangman Game using Python
- Balloon Shooter Game using Python PyGame
- Complete PyGame Tutorial and Projects
- Library Management System Python Project with source code | GUI and Database
- Beginners to Advanced Python Turtle Projects
- Flappy Bird In Python Pygame with source code
- Create Your Own Web Browser Using Python
Post Tags-