Answered step by step
Verified Expert Solution
Link Copied!

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 server-s.py below, here are the requirements:
Server handles incorrect port
Server gracefully handles SIGINT signal
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 and process 10 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 10 seconds.
Server accepts another connection after the first connection timed out
Server successfully receives a small file (~500 bytes) using the submitted version of the client (from part 1)
Server correctly saves the file from the previous test
Server successfully receives a small file (~500 bytes) using the instructors version of the client
Server correctly saves the file from the previous test
Server successfully receives sequentially 10 large files (~10 MiBytes) using the submitted version of the client (from part 1) and saves all the files from the previous test in 1.file, 2.file, ...10.file
Same as previous but in parallel
Server successfully receives sequentially 10 large files (~10 MiBytes) using the instructors version of the client (from part 1) and saves all the files from the previous test in 1.file, 2.file, ...10.file
Same as previous but in parallel
Server successfully receives sequentially 10 large files (~10 MiBytes) using the instructors version of the client (from part 1) and saves all the files from the previous test in 1.file, 2.file, ...10.file (with emulated delays and/or transmission errors)
Same as previous but in parallel
#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 "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 *
def receive_file(connectionSocket):
try:
header = connectionSocket.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 "Client closed the connection unexpectedly")
file.write(data)
bytes_received += len(data)
print(f"File received: {filename}, Size: {bytes_received} bytes")
except timeout:
print("ERROR: Timeout while receiving file")
except Exception as e:
print(f"ERROR: {e}")
def main():
serverPort =12000
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', serverPort))
serverSocket.listen(1)
print('The server is ready to receive')
while True:
connectionSocket, addr = serverSocket.accept()
with connectionSocket:
print(f"Connected by {addr}")
receive_file(connectionSocket)
print("File transfer successful")
if __name__==

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Domain Transfer Learning With 3q Data Processing

Authors: Ahmed Atif Hussain

1st Edition

B0CQS1NSHF, 979-8869061805

More Books

Students also viewed these Databases questions