Javatpoint Logo
Javatpoint Logo

File Transfer using TCP Socket in Python

What is FTP (File Transfer Protocol)?

FTP (File transfer protocol) is a network protocol for sending records between PCs over Transmission Control Protocol or protocol/internet protocol (TCP/IP) associations. Inside the TCP/IP suite, FTP is viewed as an application layer protocol. In an FTP exchange, the end client's PC is normally called the nearby host. The subsequent PC engaged with FTP is a remote host, which is normally a server. The two PCs should be associated by means of an organization and designed appropriately to move documents through FTP. Servers should be gotten up positioned run FTP administrations, and the client should have FTP programming introduced to get to these administrations.

Although many document moves can be led utilizing Hypertext Transfer Protocol (HTTP) - - one more protocol in the TCP/IP suite - - FTP is still ordinarily used to move records in the background for different applications, like financial administrations. It is likewise in some cases used to download new applications through internet browsers. FTP is a standard organization convention that can empower broad document move capacities across IP organizations. Without FTP, document and information move can be dealt with different systems - -, for example, email or a HTTP web administration - - yet those different choices come up short on lucidity of concentration, accuracy and control that FTP empowers.

FTP is utilized for document moves between one framework and another, and it has a few normal use cases, including the accompanying:

  • FTP can be utilized by backup services or individual clients to reinforcement information from one area to a got reinforcement server running FTP administrations.
  • Like reinforcement, replication includes duplication of information starting with one framework then onto the next yet adopts a more thorough strategy to give higher accessibility and flexibility. FTP can likewise be utilized to work with this.
  • Access and data stacking. FTP is likewise ordinarily used to get to shared web facilitating and cloud administrations as a component to stack information onto a far-off framework.

TCP-SERVER.py: Server-Side Implementation:

We start by bringing in the attachment library and making a straightforward socket. AF_INET alludes to the location family ipv4. The SOCK_STREAM implies association arranged TCP convention. After this, we tie the host address and port number to the server.

The client enters the greatest number of client associations required. Based on the info, we decide the size of a cluster putting away clients called Associations. The server then continues to tune in for client associations and assuming found adds them to the Associations exhibit. It does this until the Associations exhibit arrives at its greatest breaking point. Then, at that point, for each association, processing begins,

  • Data is received and afterward decoded.
  • In the event that data is not invalid, the server makes a document with the name "Output<a unique number>.txt" and composes it into it.
  • This is gone on until all information is composed into the document
  • The above occurs for all clients and afterward associations are shut. Thus, we can get another record at the server's end with precisely the same data.

Code for TCP-server.py:

TCP-CLIENT.py: Client-side Implementation:

We initially make an socket for a client and interface it to the server utilizing the tuple containing the host address and port number. Then, at that point, the Client enters the record name it needs to ship off the server. After this, utilizing the open() capability of Python, the information of the record is perused. Information is perused line-by-line and afterward encoded into double and shipped off the server utilizing socket. send(). While sending information is done, the record is shut. It continues to request a filename from the client until the client association is ended.

Code:

Output:

We originally run the server utilizing the command.

python tcp-server.py

File Transfer using TCP Socket in Python

Server-Side Result

After this, we indicate the greatest number of client associations conceivable.

  • Clients' associations are instated, i.e., the Associations exhibit is created with size 2 to store clients.
  • After this, we make another terminal and run our client utilizing the order.

python tcp-client.py

The greatest number of tabs where we can run the order in the terminal will be 2. As we are instating our client with this. So we make two clients Client 1 and Client 2.

File Transfer using TCP Socket in Python

Client-Side Result

When the client is run, we see a result "Connected with client 1" on the server side, demonstrating the association is finished effectively with the primary client. Same for the second client too.

After this, on the client side, we give the document name we need to send. Here file1.txt is sent by the two clients. The substance of file1.txt is displayed underneath:

File Transfer using TCP Socket in Python

File sent off the Server

Note that on the client side, we continue to show the brief: "Information filename you need to send:" so a client can send numerous documents. When the association with the client is separated, utilizing "Control+C" in the terminal, another record with the name Output<a number>.txt is produced on the server side with a similar substance, and the association is shut. Here, the Client 1 document is put away as Output0.txt, and the Client 2 record is put away as Ouput1.txt.

File Transfer using TCP Socket in Python

File received from Client 1

File Transfer using TCP Socket in Python

Files received from Client 2







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA