Hello, I need help with a homework assignment because I have tried to figure it out but cannot.
The assignment:
Write a Python network TCP server/client script.
It should be 1 script that can accept the arguments SERVER and run the server-side script or CLIENT and run the client-side script
Upon receiving a connection, it should send back to the client its IP address (client IP). Then it should wait for commands from the client.
Valid commands are: “File”, “TIME”, “IP”, “OS” and “EXIT”.
To the TIME command, the server should return the current time.
To the IP command, it returns the client’s IP address.
To the “OS” command, the server returns the name of its operating system and version.
To the “File” command, the server sends a dummy file to the client.
If the client closes the connection or does not respond with a command in a reasonable time (20 seconds), the server must close the current connection and wait for another connection (see Setting a timeout on a socket).
To the EXIT command, your server should close all open sockets and exit.
My issues:
- After entering 1 or 2 commands my stream no longer replies. Maybe buffer issue?
- How, HOW to close the socket after 20 seconds??? This seems to be much harder than it should be!!
My client code:
#shell_client.py for python3
import socket
import time
s= socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("192.168.135.128",4144)) #this is my local IP
filedata = bytearray()
while True:
data = s.recv(1024) #max size of data to receive
print(data.decode())
cmd = input() #allow the user to input data and fill the cmd variable
s.send(cmd.encode('utf-8')) #encode cmd and send it to the server
#if the cmd is file, receive some data, decode it and print the output
if cmd == "File":
print("cmd = File") #for debugging purposes
datafile1 = s.recv(1024)
filedata.extend(datafile1) #extend the byte array beacuse dealing with multiple packets
print (filedata.decode())
#if the cmd is time, receive some data, decode it and print the output
elif cmd == "TIME":
print("cmd = time") #for debugging purposes
timedata = s.recv(1024)
print("The current time is:", timedata.decode())
#if the cmd is os, receive some data, decode it and print the output
elif cmd == "OS":
print("cmd = os") #for debugging purposes
OSdata = s.recv(1024)
print("The OS is:", OSdata.decode())
#if the cmd is ip, receive some data, decode it and print the output
elif cmd == "IP":
print("cmd = ip") #for debugging purposes
#hostname = socket.gethostname()
#local_ip = socket.gethostbyname(hostname)
#print("Client IP is:", format(local_ip))
IPdata = s.recv(1024)
print("Client IP:", IPdata.decode())
#if the cmd is exit, close the socket and exit
elif cmd == "EXIT":
print("cmd = exit") #for debugging purposes
s.close()
break
My SERVER code:
#shell_server.py for python3
import socket
import subprocess
import time
import platform
#try to create a socket s
try:
s= socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("192.168.135.128",444)) #bind to all ip in the range
s.listen(1) #listen for 1 connection
#while the socket is created, keep the socket open
while True:
connexion, client_address = s.accept()
print ("connexion from :" +str(client_address))
#while there is a connection, ask to receive a cmd and reply accordingly
while True:
connexion.send(bytes("\nEnter cmd : ", "utf-8"))
cmd = connexion.recv(8192).decode()
# p = subprocess.Popen(cmd.split(" "),shell=True
p = subprocess.Popen(cmd,shell=True
,stdout=subprocess.PIPE,stderr = subprocess.PIPE)
out , err = p.communicate()
connexion.send(out)
#if cmd is file the open the file and display for client
if cmd == "File":
#print("Received command: File") #for debuggin purposes
p= subprocess.Popen(["cat", "file1.txt"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out,err = p.communicate()
connexion.send(out)
#if cmd is time send the current time to the client
elif cmd == "TIME":
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print("cmd = time") #for debugging purposes
connexion.send(current_time.encode())
#if the cmd is os send the os name and version to the client
elif cmd == "OS":
#print("cmd = OS") #for debugging purposes
os = platform.platform()
connexion.send(os.encode())
#if the cmd is ip .... i guess i dont need this because i want to display client ip address
elif cmd == "IP":
#print(client_address) #for debugging purposes
connexion.send(str(client_address).encode())
#if the cmd is exit then close the socket and exit (do not listen)
elif cmd == "EXIT":
#print("cmd = exit") #for debugging purposes
s.close()
break
#finally close the connection
finally:
s.close()
PLEASE any comments would be very much appreciated!!