Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Update and Edit my #server - s . pyto meet this requirements: Server gracefully handles SIGINTsignal Server accepts a connection Server accepts a connection and

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")
except timeout:
sys.stderr.write("ERROR: Timeout while receiving file
")
except Exception as e:
sys.stderr.write(f"ERROR: {e}
")
# Main function to set up the server and handle connections
def main(port):
server_socket = setup_server_socket(port)
print('The server is ready to receive')
try:
while True:
connection_socket, addr = server_socket.accept()
with connection_socket:
print(f"Connected by {addr}")
receive_file(connection_socket)
print("File transfer successful")
finally:
server_socket.close()
# Entry point of the script
if __name__=="__main__":
# Register signal handlers for graceful termination
signal.signal(signal.SIGQUIT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
# Check for proper command-line arguments
if len(sys.argv)!=2:
sys.stderr.write("ERROR: Usage: python3 server-s.py
")
sys.exit(1)
# Parse the port number from the command line
try:
server_port = int(sys.argv[1])
except ValueError:
sys.stderr.write("ERROR: Port must be an integer
")
sys.exit(1)
# Ensure port number is in the correct range
if not (0<= server_port <=65535):
sys.stderr.write("ERROR: Port number must be in the range 0-65535
")
sys.exit(1)
# Start the main function
main(server_port)

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

Mastering Apache Cassandra 3 X An Expert Guide To Improving Database Scalability And Availability Without Compromising Performance

Authors: Aaron Ploetz ,Tejaswi Malepati ,Nishant Neeraj

3rd Edition

1789131499, 978-1789131499

More Books

Students also viewed these Databases questions

Question

7. Identify six intercultural communication dialectics.

Answered: 1 week ago