Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a file transfer application using TCP sockets in Python. The file-transfer

ID: 3749181 • Letter: W

Question

Write a file transfer application using TCP sockets in Python. The file-transfer protocol will include a server called ftps.py and a client called ftpc.py. In the following, we assume the client is running on beta.cse.ohio-state.edu and the server is running on gamma.cse.ohio-state.edu.1 First, start the server on gamma.cse.ohio-state.edu using the command:

python3 ftps.py Then, start ftpc.py on beta.cse.ohio-state.edu with the command:

The ftpc.py client will send all bytes of that local file. The ftps.py server should receive the file and then store it. Make sure that the new file created by ftps.py is in a different directory to avoid overwriting the original file since all CSE machines have your root directory mounted.

The file-transfer application will use a simple protocol. The first 4 bytes (in network byte order) will contain the number of bytes in the file to follow. The next 20 bytes will contain the name of the file (assume the name fits in 20 bytes). The rest of the bytes in the TCP stream to follow will contain the data in the file. Note that TCP is not packet-oriented, but it is stream-oriented. Submit well-documented code using the following command:

Your program should work for binary files (images, etc.) of arbitrary size. “Chunk” the file into “pieces,” each of which is at most 1,000 bytes. Submit a README.txt file with your lab. The client and server should be running on different machines on stdlinux (see the footnote). You can use the sample images on the course webpage to test your program. Use diff or md5sum to ensure

that the transferred file is the same as the original one.

1In your lab, you can substitute any machine on stdlinux (e.g., zeta.cse.ohio-state.edu or epsilon.cse.ohio-state.edu) for beta.cse.ohio-state.edu and gamma.cse.ohio-state.edu, respectively. You should do so to avoid “overloading” these machines.

FYI: stdlinux.cse.ohio_state is the server of linux environment in ohio state. The program may not necessary run on that machine. Just need the general answer of TCP implementation. I don't know how to use python to implement TCP. Thanks!

Explanation / Answer

Python File Transfer over TCP :

FileTransferTCPClient :

import socket                   # Import socket module

s = socket.socket()              # Create a socket object

host = "1somehing.11somehing." #Ip address that the TCPServer is there

port = 50000                      # Reserve a port for your service every new transfer wants a new port or you must wait.

s.connect((host, port))

s.send("Hello server!")

with open('received_file', 'wb') as f:

    print 'file opened'

    while True:

        print('receiving data...')

        data = s.recv(1024)

        print('data=%s', (data))

        if not data:

            break

        # write data to a file

        f.write(data)

f.close()

print('Successfully get the file')

s.close()

print('connection closed')

*******************************************************************************************************

FileTransferTCPServer :

import socket                   # Import socket module

port = 50000                    # Reserve a port for your service every new transfer wants a new port or you must wait.

s = socket.socket()             # Create a socket object

host = ""   # Get local machine name

s.bind((host, port))            # Bind to the port

s.listen(5)                     # Now wait for client connection.

print 'Server listening....'

while True:

    conn, addr = s.accept()     # Establish connection with client.

    print 'Got connection from', addr

    data = conn.recv(1024)

    print('Server received', repr(data))

    filename='TCPSERVER.py' #In the same folder or path is this file running must the file you want to tranfser to be

    f = open(filename,'rb')

    l = f.read(1024)

    while (l):

       conn.send(l)

       print('Sent ',repr(l))

       l = f.read(1024)

    f.close()

    print('Done sending')

    conn.send('Thank you for connecting')

    conn.close()


***********************************************************************************************************

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote