Control PC from anywhere using Python

Control PC from anywhere using Python

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.

FunctionDescription
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

Control PC from anywhere using Python, server side output
server side

After executing the clientprog.py

Control PC from anywhere using Python for client side
client side

Here we have successfully formed the connection between two nodes on the network.

For more such articles on python keep visiting our website www.violet-cat-415996.hostingersite.com.

Thank you for reading this article.


Also Read:

Share:

Author: Ayush Purawr