Previously we discussed networking in python today. We will practically implement how networking works in python. It is a straightforward technique, so let’s start.
Command Line Trick to Share Files:
Suppose you are sitting in a classroom, and all the students are connected with the same WIFI or internet, and your professor wants to share a file that is 2GB in size. It will be very time consuming if a professor shares it with the USB device. We can create a shared folder, but still, if someone wants it on his her mobile or someone using some other OS, then there will be a possibility that conflict arises. In such a situation, we can transfer the data to any device, whether an Android device, IOS, Linux operating system, or windows, with just one command line operation as follows:
• Open your terminal.
• Navigate to that folder which you want to share. It is to be noted never share a root folder as it will broadcast all the files of your computer. Always create a separate folder and put the files within it and then share that folder only.
• After navigating to that particular folder, write the following command:
For window users:
python -m http.server
For Linux User:
python3 -m http.server
• Now open another terminal and see what’s your IP is. The following command is to be made to know the IP address of the system.
For window user:
For Linux and Mac user:
• Open your browser and enter the IP address of the system from which folder has been shared and the port at which it is running.
The concept can be more transparent by the images as follows:
This is the folder that I want to share. This folder contains a python project based on hand gesture recognition. Now I open a terminal and write the above mention command in it, as shown in figure 2.
After running a command, it will say the server is running at port 8000. It is a default port. We can also change the port number, but it is not needed. Then open any web browser and write the IP address: port in a URL section will be shared with all of the users.
As shown in figure 3, the user can see all the folders. We can also click these folders to see what’s inside. If you want to download any particular folder, then you have to zip it before sharing, which will make it enable you to download it on a click.
TCP in Python:
TCP in python is as easy as creating the HTTP server for file sharing. For creating a TCP server, two files are necessary to create one is a server, and the other one is the client file. The server file is initiated first and listens for the new connection; once the connection is established, it can send and receive the data. Following is the example of TCP as python code.
sock = socket.socket()
data = "hello world"
It is the client file. In the first line, we import the socket module of python. The socket is built in the module, so there is no need to install a socket library via pip. Inline second we create an instance of the socket. It is to be noted that we do not give any argument in socket class initialization because it is by default TCP.
In the 3rd line, we connect the socket with IP address and port. The connect function takes the tuple of IP and port than we send the data by the send function. It can only send the byte, so we have to convert it into bytes first.
s = socket.socket()
print('Server start listening')
c,add = s.accept()
data = c.recv(1024)
In the server script, we first import sockets and initialize the instance of socket class. It is to be noted that if there is a TCP connection of the client file, then there must be a TCP socket on a server script.
After initializing, we bind the socket at IP address and port. It will bind the socket and fill the slot of that particular port so that no other function could perform on this port. If someone is accessing from another device, then an IP address is to be placed instead of using 127.0.0.1. Always use the port above 5000 because below 5000 ports are usually system reserved ports, and it may crash your operating system if you try to kill that port.
After binding it to the port, the socket starts listening on that port. The number 5 here indicated the number of users, which means five users can connect to that particular IP address and port at a time. You can increase the number of users according to the need.
At the next step, if someone requests that the mean sockets connection request is made, the program enters into a while loop and accepts the connection, receives the data that is encoded, so we first have to decode the data before printing the data.
Now to run this app, we first have to run the server file and then the client file.
UDP stands for user datagram protocol. It is similar to the TCP concerning coding in python; otherwise, it is different. To initialize the UDP, we have to give the parameters while initializing the socket class. In python UDP, only a few bytes of data can be transferred, for a large piece of data, it is not feasible, or you have to use multiple packets to send the data over UDP.
Moreover, there is a little difference in code. In TCP, we use the send() and recv() function to send and receives the data, while in UDP, the functions name is changed. In UDP it is sendto() and recvfrom() instead of send() and recv().