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 server-s.py, should accept a command-line argument for the port number on which it will listen for connections from any interface.
Upon accepting a connection, the server sends the accio\r
command to the client, receives a confirmation, sends the accio\r
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 0.0.0.0 IP address.Process errors related to incorrect port numbers gracefully, exiting with a non-zero 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 10 simultaneous connections without using multithreading (using the correct parameter for socket.listen).Assume an error if no data is received from the client for over 10 seconds, abort the connection, and write an ERROR message instead of the number of bytes read.Accept large file transfers (100 MiB or more) without requiring the entire file to fit in memory.
The server should:
Not open files in "text" mode and avoid using .decode('utf-8') or .encode('utf-8') in the program.Use signal handlers for SIGQUIT, SIGTERM, and SIGINT signals without using sys.exit() to ensure graceful termination.Include a not_stopped 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 accio\r
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 connectTcp(host, port):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10)
sock.connect((host, port))
return sock
except socket.error as e:
sys.stderr.write(f"ERROR: Connection failed due to {e}
")
sys.exit(1)
def receive_commands_and_confirm(sock):
expected_command = b"accio\r
"
command_buffer = b""
commands_received =0
while commands_received <2:
try:
data = sock.recv(1)
if not data:
raise Exception("Server closed the connection unexpectedly")
command_buffer += data
if command_buffer.endswith(expected_command):
if commands_received ==0:
sock.sendall(b"confirm-accio\r
")
elif commands_received ==1:
sock.sendall(b"confirm-accio-again\r
")
sock.sendall(b"\r
")
commands_received +=1
command_buffer = b""
except socket.timeout:
sys.stderr.write("ERROR: Timeout while waiting for commands
")
sys.exit(1)
except Exception as e:
sys.stderr.write(f"ERROR: {e}
")
sys.exit(1)
def send_file(sock, filename, save_as):
try:
with open(filename,'rb') as "ERROR: Failed to send file due to {e}
")
sys.exit(1)
def main():
if len(sys.argv)!=4:
sys.stderr.write("Usage:
")
sys.exit(1)
host = sys.argv[1]
try:
port = int(sys.argv[2])
if not (0<= port <=65535):
raise ValueError("Port number must be in the range 0-65535.")
except ValueError as e:
sys.stderr.write(f"ERROR: {e}
")
sys.exit(1)
filename = sys.argv[3]
save_as = f"{host.replace('.','_')}_{port}_{filename}"
sock = connectTcp(host, port)
receive_commands_and_confirm(sock)
send_file(sock, filename, save_as)
print(f"File transfer successful, saved as {save_as}")
sock.close()
if __name__=="__main__":
main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!