Question: Update and Edit my #server - s . py to meet this requirements: Server gracefully handles SIGINT signal Server accepts a connection Server accepts a

Update and Edit my #server-s.pyto meet this requirements:
Server gracefully handles SIGINTsignal
Server accepts a connection
Server accepts a connection and sends accio\r
command
Server starts receiving data
Server accepts another connection after the first connection finished
When server receives 10 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 10 seconds.
Server accepts another connection after the first connection timed out
Server successfully receives a small amount of data (~500 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 (~10 MiBytes) using the instructors version of the client (without emulated delays and/or transmission errors)
Server prints the correct value for the received data from the previous test
Server successfully receives a large amount of data (~10 MiBytes) using the instructors version of the client (withemulated delays and/or transmission errors)
Server prints the correct value for the received data from the previous test
#client.py
import sys
import socket
def connectTcp(host, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10)
try:
sock.connect((host, port))
except socket.gaierror:
sys.stderr.write("ERROR: Invalid hostname or service not known
")
sys.exit(1)
except socket.timeout:
sys.stderr.write("ERROR: Connection attempt timed out
")
sys.exit(1)
except socket.error as e:
sys.stderr.write(f"ERROR: {e}
")
sys.exit(1)
return sock
def receive_commands_and_confirm(sock):
expected_command = b"accio\r
"
command_buffer = b""
commands_received =0
while commands_received <2:
data = sock.recv(1)
if not data:
sys.stderr.write("ERROR: Server closed the connection unexpectedly
")
sys.exit(1)
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""
def send_file(sock, filename):
with open(filename,'rb') as file:
file_data = file.read()
sock.sendall(file_data)
def main():
if len(sys.argv)!=4:
sys.stderr.write("ERROR: Usage:
")
sys.exit(1)
host = sys.argv[1]
try:
port = int(sys.argv[2])
except ValueError:
sys.stderr.write("ERROR: Port must be an integer
")
sys.exit(1)
if not (0<= port <=65535):
sys.stderr.write("ERROR: Port number must be in the range 0-65535
")
sys.exit(1)
filename = sys.argv[3]
try:
sock = connectTcp(host, port)
receive_commands_and_confirm(sock)
send_file(sock, filename)
print("File transfer successful")
sock.close()
except Exception as e:
sys.stderr.write(f"ERROR: {e}
")
sys.exit(1)
if __name__=="__main__":
main()
#server-s.py
from socket import *
import sys
import signal
import os
# Signal handler for graceful shutdown
def signal_handler(signum, frame):
sys.exit(0)
# Set up and return the server socket
def setup_server_socket(port):
server_socket = socket(AF_INET, SOCK_STREAM)
server_socket.bind(('0.0.0.0', port))# Bind to all interfaces
server_socket.listen(10)# Set up queue for up to 10 connections
return server_socket
# Handle the process of receiving a file
def receive_file(connection_socket):
try:
header = connection_socket.recv(10)# assuming header size is 10 bytes
filesize = int.from_bytes(header, byteorder='big')
bytes_received =0
filename = "received_file.txt"# Change the filename as needed
with open(filename,'wb') as file:
while bytes_received < filesize:
data = connection_socket.recv(min(1024, filesize - bytes_received))
if not data:
raise Exception("Client closed the connection unexpectedly")
file.write(data)
bytes_received += len(data)
print(f"File received: {filename}, Size: {bytes_received} bytes")

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!