This mini-project should be forked. Forking the repo will create a copy on your own GitHub repo. Once the repo is forked, you can clone YOUR repo using PyCharm. Submission entails committing and pushing your changes to your repo.
This exercise aims to create a simple TCP file transfer system, where a client will send a file to a server, and the server will receive the file and save it to disk. There are no limits on the size of the file.
In this mini-project, you will implement a simple protocol for file transfer over TCP. Because the socket is UDP, the application layer is responsible for ensuring that the file is transferred without error. While this algorithm addresses some of the reliability transfer issues, many problems related to reliable transfer still need to be addressed.
Note: This mini-project is intended to provide practice in socket programming. File transfer should be done over a TCP socket. This will be the subject of the next mini-project.
The algorithms described below implement the following simple protocol.
Figure 1: Simple file transfer protocol.The server should:
- Create a socket using the
socket
function and bind it to the (ip, port) pair. (Implemented) - Receive a message from the client using the
recv
function. - Decompose the message into an 8-byte integer representing file size. The rest of the bytes should be decoded into a string representing the file name. Use
get_file_info()
. - send
b'go ahead'
message to the client. - Using the filename provided, open the file for writing using the
with
python statement. NOTE: since you will be transferring a file over localhost to the same directory, it is important that you modify the filename, say by adding a .temp extension, to avoid overwriting the original file. (Implemented) - Inside the with statement,
- Receive a chunk of data.
- Update the number of bytes received.
- Write the data to the file.
- Continue to perform steps 7a-c until all bytes have been received.
- If an exception occurs, the file should be deleted. (Implemented)
- Close the client socket. (Implemented)
- Repeat steps 2 - 8. (Implemented)
The client should:
- Obtain the name of the file to transfer from the command line using sys.argv. (Implemented)
- Obtain the size of the file in bytes by using the os.path module.(use
get_file_size()
) - Convert the file size to an 8-byte string using big endian.
- Create a TCP socket using the
socket
function. (Implemented) - Connect to the server using the (ip, port) pair.
- Send the 8-byte file size concatenated with the encoded filename to the server.
- Wait for the server to send
b'go ahead'
message. If any other message is sent,raise OSError('Bad server response - was not go ahead!')
- Open the file for reading in binary mode using the with python statement.
- Read a chunk of data from the file.
- If the length of the chunk > 0, then send the chunk to the server else go to step 9. No more chunks to read and send.
- Repeat steps 8a - 8b
- Close the client socket. (Implemented)
You can test your implementation by running the server and client on the same machine. Make sure to run the server first and then the client. NOTE: as mentioned in section 1 above, it is important that you modify the filename, say by adding a .temp extension, to avoid overwriting the original file. This has already been done on the server's with statement. Such a filename change is only necessary when running both server and client on the same machine and using the same working directory. You do not have to modify the filename if you set the run configuration of the server to use a different working directory from that of the client.
You are required to commit and push your code to your forked repo.
- Make sure to handle errors in your code.
- Pay attention to the TODO tasks and how they are mapped to the algorithm steps above. This will help guide you in implementing the code.
- Read the Python documentations for the sys, os, and os.path modules along with associated examples. Avoid Googling the answer as this will get you into the habit of reading documentaton.
Good luck and happy coding!