Question: Update code below to meet requirements: The server, implemented in server - s . py , should accept a command - line argument for the
Update code below to meet requirements:
The server, implemented in serverspy should accept a commandline argument for the port number on which it will listen for connections from any interface.
Upon accepting a connection, the server sends the accior
command to the client, receives a confirmation, sends the accior
command again, then receives a binary file from the client, counts the number of bytes received excluding the header size and prints out this number.
The server must:
Open a listening socket on the specified port using the socket.bind method with the IP address.Process errors related to incorrect port numbers gracefully, exiting with a nonzero error code and printing an error message starting with ERROR:.Exit with code zero when receiving SIGQUIT, SIGTERM, or SIGINT signals.Handle multiple connections sequentially and up to simultaneous connections without using multithreading using the correct parameter for socket.listenAssume an error if no data is received from the client for over seconds, abort the connection, and write an ERROR message instead of the number of bytes read.Accept large file transfers MiB or more without requiring the entire file to fit in memory.
The server should:
Not open files in "text" mode and avoid using decodeutf or encodeutf in the program.Use signal handlers for SIGQUIT, SIGTERM, and SIGINT signals without using sysexit to ensure graceful termination.Include a notstopped variable set to True to keep running until a signal sets it to False.
The implementation should include routines to:
Initiate the server socket.Accept a connection.Send accior
after a connection is established.Test the server using telnet or a client application to ensure it accepts connections correctly.
Error handling and signals processing should be managed appropriately to meet the requirements for a smooth and correct server operation.
Remember to follow the specifications closely and test the server to ensure that it meets all the requirements and handles edge cases as expected.
import sys
import socket
def connectTcphost port:
try:
sock socket.socketsocketAFINET, socket.SOCKSTREAM
sock.settimeout
sock.connecthost port
return sock
except socket.error as e:
sysstderr.writefERROR: Connection failed due to e
sysexit
def receivecommandsandconfirmsock:
expectedcommand baccior
commandbuffer b
commandsreceived
while commandsreceived :
try:
data sock.recv
if not data:
raise ExceptionServer closed the connection unexpectedly"
commandbuffer data
if commandbuffer.endswithexpectedcommand:
if commandsreceived :
sock.sendallbconfirmaccior
elif commandsreceived :
sock.sendallbconfirmaccioagainr
sock.sendallbr
commandsreceived
commandbuffer b
except socket.timeout:
sysstderr.writeERROR: Timeout while waiting for commands
sysexit
except Exception as e:
sysstderr.writefERROR: e
sysexit
def sendfilesock filename, saveas:
try:
with openfilenamerb as "ERROR: Failed to send file due to e
sysexit
def main:
if lensysargv:
sysstderr.writeUsage:
sysexit
host sysargv
try:
port intsysargv
if not port :
raise ValueErrorPort number must be in the range
except ValueError as e:
sysstderr.writefERROR: e
sysexit
filename sysargv
saveas fhostreplaceportfilename
sock connectTcphost port
receivecommandsandconfirmsock
sendfilesock filename, saveas
printfFile transfer successful, saved as saveas
sock.close
if namemain:
main
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
