GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing thepythoncode.com — How to Transfer Files in the Network using Sockets in Python - The Python Code Confused by complex code? Let our AI-powered Code Explainer demystify it for you. Try it out! File transfer is the process of copying or moving a file from one computer to another over a network or Internet connection. In this tutorial, we'll go over how you can write client/server Python scripts that handle that step by step. The basic idea is to create a server that listens on a particular port; this server will be responsible for receiving files (you can make the server send files as well). On the other hand, the client will try to connect to the server and send a file of any type. We will use the Please note that there are more reliable ways to transfer files with tools like Related: How to Organize Files by Extension in Python. First, we gonna need to install the
Client CodeLet's start with the client code, the code that is responsible for sending:
We need to specify the server IP address, the server port we want to connect to, and the file name we want to send:
The The
Let's create the TCP socket:
Connecting to the server:
I've used Now we need to send the file, and as we are sending the file, we'll print nice progress bars using the
Basically, what we are doing here is opening the file as read in binary ( Related: How to Make a Chat Application in Python. Server CodeAlright, so we are done with the client. Let's dive into the server, so open up a new empty Python file and:
I've initialized some parameters we are going to use. Notice that I've used "0.0.0.0" as the server IP address. This means all IPv4 addresses that are on the local machine. You may wonder why we don't just use our local IP address or "localhost" or "127.0.0.1"? Well, if the server has two IP addresses, let's say "192.168.1.101" on one network and "10.0.1.1" on another, and the server listens on "0.0.0.0", it will be reachable at both of those IPs. Alternatively, you can use your public or private IP address, depending on your clients. If the connected clients are in your local network, you should use your private IP (you can check it using Also, make sure you use the same port on the server as on the client. Let's create our TCP socket:
Now, this is different from the client; we need to bind the socket we just created to our
After that, we are going to listen for connections:
Once the client connects to our server, we need to accept that connection:
Remember that when the client is connected, it'll send the name and size of the file. Let's receive them:
As mentioned earlier, the received data is combined with the After that, we need to remove the file's absolute path because the sender sent the file with his own file path, which may differ from ours; the Now we need to receive the file:
It is not entirely different from the client code. However, we are opening the file as write in binary ( Learn also: How to List all Files and Directories in FTP Server using Python Alright, let me try it on my own private network:
I need to go to my Linux box and send an example file:
Let's see the server now:
ConclusionGreat, we are done! It's working! You can get the complete code here. If you wish to run the server code on a remote machine and not on the local network, then make sure you allow the port on your firewall. If it's a VM in the cloud, then make sure you allow it via
This will tell the firewall to allow that port for remote communication. If the server is in your home, you must enable the port on your router settings, which you can typically access using the web browser via the router's IP address, typically 192.168.1.1. You can check the default gateway IP (router's IP) using the You can extend this code for your own needs now. Here are some examples you can implement:
Read Also: How to Manipulate IP Addresses in Python. Happy Coding ♥ Why juggle between languages when you can convert? Check out our Code Converter. Try it out today!
Sharing is caring!
Read also
How to Organize Files by Extension in PythonLearn how to separate your files by extension in your target folder using Python's built-in libraries.
How to Create a Reverse Shell in PythonBuilding a reverse shell in Python using sockets that can execute remote shell commands and send the results back to the server. How to Download Files from URL in PythonLearn how to use requests and tqdm libraries to build a powerful file downloader with progress bar using Python. CommentsGot a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips! |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |