Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please edit and debug a server.py code in to complement with client.py and server - s . py below, here are the requirements: Server handles
Please edit and debug a server.py code in to complement with client.py and serverspy below, here are the requirements:
Server handles incorrect port
Server gracefully handles SIGINT signal
Server accepts a connection
Server accepts a connection and sends accior
command
Server starts receiving data
Server accepts another connection after the first connection finished
When server receives and process connections simultaneously
Server aborts connection and write ERROR into corresponding file resetting any received content when it does not receive new data from client for more than seconds.
Server accepts another connection after the first connection timed out
Server successfully receives a small file ~ bytes using the submitted version of the client from part
Server correctly saves the file from the previous test
Server successfully receives a small file ~ bytes using the instructors version of the client
Server correctly saves the file from the previous test
Server successfully receives sequentially large files ~ MiBytes using the submitted version of the client from part and saves all the files from the previous test in file, file, file
Same as previous but in parallel
Server successfully receives sequentially large files ~ MiBytes using the instructors version of the client from part and saves all the files from the previous test in file, file, file
Same as previous but in parallel
Server successfully receives sequentially large files ~ MiBytes using the instructors version of the client from part and saves all the files from the previous test in file, file, file with emulated delays andor transmission errors
Same as previous but in parallel
#client.py
import sys
import socket
def connectTcphost port:
sock socket.socketsocketAFINET, socket.SOCKSTREAM
sock.settimeout
try:
sock.connecthost port
except socket.gaierror:
sysstderr.writeERROR: Invalid hostname or service not known
sysexit
except socket.timeout:
sysstderr.writeERROR: Connection attempt timed out
sysexit
except socket.error as e:
sysstderr.writefERROR: e
sysexit
return sock
def receivecommandsandconfirmsock:
expectedcommand baccior
commandbuffer b
commandsreceived
while commandsreceived :
data sock.recv
if not data:
sysstderr.writeERROR: Server closed the connection unexpectedly
sysexit
commandbuffer data
if commandbuffer.endswithexpectedcommand:
if commandsreceived :
sock.sendallbconfirmaccior
elif commandsreceived :
sock.sendallbconfirmaccioagainr
sock.sendallbr
commandsreceived
commandbuffer b
def sendfilesock filename:
with openfilenamerb as "ERROR: Usage:
sysexit
host sysargv
try:
port intsysargv
except ValueError:
sysstderr.writeERROR: Port must be an integer
sysexit
if not port :
sysstderr.writeERROR: Port number must be in the range
sysexit
filename sysargv
try:
sock connectTcphost port
receivecommandsandconfirmsock
sendfilesock filename
printFile transfer successful"
sock.close
except Exception as e:
sysstderr.writefERROR: e
sysexit
if namemain:
main
#serverspy
from socket import
def receivefileconnectionSocket:
try:
header connectionSocket.recv # assuming header size is bytes
filesize int.frombytesheader byteorder'big'
bytesreceived
filename "receivedfile.txt # Change the filename as needed
with openfilenamewb as "Client closed the connection unexpectedly"
file.writedata
bytesreceived lendata
printfFile received: filename Size: bytesreceived bytes"
except timeout:
printERROR: Timeout while receiving file"
except Exception as e:
printfERROR: e
def main:
serverPort
serverSocket socketAFINET, SOCKSTREAM
serverSocket.bind serverPort
serverSocket.listen
printThe server is ready to receive'
while True:
connectionSocket, addr serverSocket.accept
with connectionSocket:
printfConnected by addr
receivefileconnectionSocket
printFile transfer successful"
if name
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started