Control Your PC From Anywhere
Control Your PC From Anywhere
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
------------/******/*/***/
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.
soc = socket.socket()
host = socket.gethostname()
port = 8080
soc.bind(('', port))
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.
import time
import socket
import sys
import os
conn.send(command.encode())
if data:
print("command received and executed successfully.")