Python connect to socket file

Python Socket File Transfer Send

The intention of this article is to learn how to transfer a text file over network through python program. This file transfer is based on server client model to use socket programming in python3+.

Basic Set up Diagram:

Here is the basic set up diagram to run this program.

For simplicity we will call System A as A_client and System B as B_server throughout the article.

File requirements:

We need server.py and this file should be present at server system. In our case server.py should be at B_server system.

Another two files client.py and sample.txt should be present at client system. In our case those two files should be present at A_client system.

Assumptions:

  • We should have two Linux systems with terminal access.
  • Preferable Linux flavor is Ubuntu.
  • Python3 should be installed.
  • Both Linux systems should able to ping each other. Use ping command to check ping.
  • One system should act as Server and other system should act as client at one particular time.

Limitations:

Before we proceed further we should know that there are some limitations of this program.

  • Python3+ should be installed to run this program. You may observe error or different behavior if run on python older versions.
  • Only text file can be transferred through this program as of now. Any other format file which does not contain text may fail.
  • Basic programming exceptions have been handled in the program.
  • Program may or may not run on other OS than Ubuntu.
  • Text file should be short at client side as buffer size of 1024 bytes has been used.

Set up requirements:

  • We need at least one Linux system to try out this program. But recommendation is to use two different Linux systems which are connected through network.
  • Two systems should be connected through Ethernet or Wi-Fi or any other connections.

Server Source Code:

Client Source Code:

# Importing libraries
import socket
import sys

# Lets catch the 1st argument as server ip
if ( len ( sys . argv ) > 1 ) :
ServerIp = sys . argv [ 1 ]
else :
print ( » \n \n Run like \n python3 client.py \n \n » )
exit ( 1 )

# Now we can create socket object
s = socket . socket ( )

# Lets choose one port and connect to that port
PORT = 9898

# Lets connect to that port where server may be running
s. connect ( ( ServerIp , PORT ) )

# We can send file sample.txt
file = open ( «sample.txt» , «rb» )
SendData = file . read ( 1024 )

while SendData:
# Now we can receive data from server
print ( » \n \n ################## Below message is received from server ################## \n \n » , s. recv ( 1024 ) . decode ( «utf-8» ) )
#Now send the content of sample.txt to server
s. send ( SendData )
SendData = file . read ( 1024 )

# Close the connection from client side
s. close ( )

How to run programs and expected output:

Here are the steps to execute the program.

Step1: Go to B_server system and open a terminal. Short cut to open a terminal is Alt+Ctrl+t.

Step2: Now go the path where server.py is present.

Step3: Now run server.py like below

There should not be any errors and you should see below prints

Copied file name will be recv.txt at server side

Step4: Now open terminal at A_client system.

Step5: Go to the path where client.py and sample.txt are present.

Step6: Now run client.py like below

We have observed that we need to know the IP address of server. We can execute below command to know the IP address of B_server system.

Now output of A_client system should be like this

Step7: Now go to B_server and look for below output

Step8: There should be one file name recv.txt at server folder. The content of this recv.txt should be same sample.txt.

So we have successfully copied a file from client to server over network through python program.

Code explanations:

There are two python files server.py and client.py.

Note that we will explain once if any code is same inside server.py and client.py.

This is shebang line which means by default this server.py should use python3. Let’s see one advantage of this line.

We have executed the server.py or client.py like python3 . Now without using python3 we can execute the python file. Follow below commands

Give all permission to .py file:

Источник

Читайте также:  Openserver php extension http
Оцените статью