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__=="__main__":
main()
#server.py
import sys
import os
import signal
import socket
import time
def signal_handler(sig, frame):
print("
Exiting...")
sys.exit(0)
def receive_file(connectionSocket, file_dir, connection_id):
try:
filename = os.path.join(file_dir, f"{connection_id}.file")
with open(filename,'wb') as "File received: {filename}, Size: {os.path.getsize(filename)} bytes")
except socket.timeout:
print(f"ERROR: Timeout while receiving file for connection {connection_id}")
with open(filename,'wb') as "ERROR")
except Exception as e:
print(f"ERROR: {e}")
def main():
if len(sys.argv)!=3:
sys.stderr.write("ERROR: Usage: python3 server.py
")
sys.exit(1)
try:
port = int(sys.argv[1])
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)
file_dir = sys.argv[2]
if not os.path.isdir(file_dir):
sys.stderr.write("ERROR: Specified directory does not exist
")
sys.exit(1)
signal.signal(signal.SIGQUIT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverSocket.bind(('0.0.0.0', port))
serverSocket.listen(10)
print(f"Server is listening on port {port}...")
connection_id =1
while True:
connectionSocket, addr = serverSocket.accept()
print(f"Connected by {addr}, Connection ID: {connection_id}")
connectionSocket.settimeout(10)
receive_file(connectionSocket, file_dir, connection_id)

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

Step: 3

blur-text-image

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

Entity Alignment Concepts Recent Advances And Novel Approaches

Authors: Xiang Zhao ,Weixin Zeng ,Jiuyang Tang

1st Edition

9819942527, 978-9819942527

More Books

Students also viewed these Databases questions

Question

(4) C (U + 2102), the set of complex numbers; and

Answered: 1 week ago