Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Update and Edit my #server - s . pyto meet this requirements: Server gracefully handles SIGINTsignal Server accepts a connection Server accepts a connection and
Update and Edit my #serverspyto meet this requirements:
Server gracefully handles SIGINTsignal
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 connections simultaneously, it accepts and process them sequentially without rejecting
Server aborts connection and prints ERROR when it does not receive data from client for more than seconds.
Server accepts another connection after the first connection timed out
Server successfully receives a small amount of data ~ bytes using the instructors version of the client
Server prints the correct value for the received data from the previous test
Server successfully receives a large amount of data ~ MiBytes using the instructors version of the client without emulated delays andor transmission errors
Server prints the correct value for the received data from the previous test
Server successfully receives a large amount of data ~ MiBytes using the instructors version of the client withemulated delays andor transmission errors
Server prints the correct value for the received data from the previous test
#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 file:
filedata file.read
sock.sendallfiledata
def main:
if lensysargv:
sysstderr.writeERROR: 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
import sys
import signal
import os
# Signal handler for graceful shutdown
def signalhandlersignum frame:
sysexit
# Set up and return the server socket
def setupserversocketport:
serversocket socketAFINET, SOCKSTREAM
serversocket.bind port # Bind to all interfaces
serversocket.listen # Set up queue for up to connections
return serversocket
# Handle the process of receiving a file
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 file:
while bytesreceived filesize:
data connectionsocket.recvmin filesize bytesreceived
if not data:
raise ExceptionClient closed the connection unexpectedly"
file.writedata
bytesreceived lendata
printfFile received: filename Size: bytesreceived bytes"
except timeout:
sysstderr.writeERROR: Timeout while receiving file
except Exception as e:
sysstderr.writefERROR: e
# Main function to set up the server and handle connections
def mainport:
serversocket setupserversocketport
printThe server is ready to receive'
try:
while True:
connectionsocket, addr serversocket.accept
with connectionsocket:
printfConnected by addr
receivefileconnectionsocket
printFile transfer successful"
finally:
serversocket.close
# Entry point of the script
if namemain:
# Register signal handlers for graceful termination
signal.signalsignalSIGQUIT, signalhandler
signal.signalsignalSIGTERM, signalhandler
signal.signalsignalSIGINT, signalhandler
# Check for proper commandline arguments
if lensysargv:
sysstderr.writeERROR: Usage: python serverspy
sysexit
# Parse the port number from the command line
try:
serverport intsysargv
except ValueError:
sysstderr.writeERROR: Port must be an integer
sysexit
# Ensure port number is in the correct range
if not serverport :
sysstderr.writeERROR: Port number must be in the range
sysexit
# Start the main function
mainserverport
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