Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

from socket import * from struct import pack, unpack import os , time, sys , logging , hashlib, math Protocol Description 1

from socket import *
from struct import pack, unpack
import os, time, sys, logging, hashlib, math
"""
Protocol Description
1. Client initiates connection with the server
2. Client sends a request to the server to transfer a file. This request includes
* the name of the file
* the size of the file
* the chunk size to be used during transfer
* the checksum length used to validate the file chunks
3. Server responds with an ACK indicating it is ready to receive the file. The ACK uses the following format
* ACK type
* the next expected chunk number. This should be zero here as the server needs the first chunk
4. The client begins sending chunks of the file to the server. Chunks use the following format
* the chunk number
* the current file chunk
* a checksum of the other data in this chunk
5. The server receives chunks and sends an ACK message. The ACK uses the following format:
* ACK type
* the next expected chunk number. Note: if the packet is received successfully, this is the next packet number. If the packet was NOT received successfully (i.e. the checksum didn't validate) then this should be the previously expected chunk number
6. This continues until the entire file has been received and the server saves the file and sends a final ACK to the client. The client can then either send another file, or close the connection and quit.
"""
class FileTransferClient:
def __init__(self, host, port, chunk_size=32768, hash_length=8):
"""
Initializes the FileTransferClient.
This constructor sets up the logging configuration for the client and stores and provided parameters.
Parameters:
host (string): the hostname or IP address to connect to
port (int): The port number to connect TODO -
chunk_size (int): the number of bytes in each chunk to use when transmitting the file
hash_length (int): the length of the hash in bytes
Note:
The logger setup provided should not be modified and is used for outputting
status and debugging messages.
"""
# Do not modify this logger code. You can continue to use print() commands for your code.
# The logger is in place to ensure that you receive proper output and debugging messages from the tester
self.logger = logging.getLogger("CLIENT")
self.handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter("%(asctime)s -%(levelname)s -%(message)s")
self.handler.setFormatter(formatter)
self.logger.addHandler(self.handler)
self.logger.propagate = False
self.logger.info("Initializing client...")
# End of logger code
self.host = host
self.port = port
self.chunk_size = chunk_size
self.hash_length = hash_length
self.client_socket = None
def start(self):
"""
Initiates a connection to the server and marks the client as running. This function
does not attempt to send or receive any messages to or from the server.
This method is responsible for establishing a socket connection to the specified
server using the host and port attributes set during the client's initialization.
It creates a TCP socket object and attempts to connect to the server. It logs
the connection attempt and reports any errors encountered during the process.
Returns:
True or False depending on whether the connection was successful.
Exceptions:
- OSError: Raised if the socket encounters a problem (e.g., network
issue, invalid host/port).
Usage Example:
client = FileTransferClient(host='192.168.1.10', port=12345, hash_length=8)
if client.start():
... do something ...
Note:
- It is important to call this method before attempting to send or receive
messages using the client.
- Ensure that the server is up and running at the specified host and port
before calling this method.
"""
# TODO: Implement the functionality described in this function's docstring
pass
def read_file(self, filepath):
"""
Reads the contents of a file and returns the data as a byte object.
This is a helper function. You do not need to modify it. You can use it in the send_file
method to read the contents of a file and return the data as a byte object.
"""
with open(filepath,"rb") as file:
data = file.read()
return data
def send_file(self, filepath):
"""
Sends a file to the server using the established connection.
File Transfer Protocol:
1. Send a file transfer request to the server. Generate this request using the
pack_transfer_request_message function
2. Receive

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

MySQL/PHP Database Applications

Authors: Jay Greenspan, Brad Bulger

1st Edition

ISBN: 978-0764535376

More Books

Students also viewed these Databases questions

Question

6. Are my sources reliable?

Answered: 1 week ago

Question

5. Are my sources compelling?

Answered: 1 week ago