Network Programming Practical in Python

In this part of Learning Python we Cover Network Programming Practical in Python in Detail with Examples
Written by Paayi Tech |24-Apr-2019 | 0 Comments | 141 Views

Previously we discussed on 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 which 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. Following command is to be made to know the IP address of the system.

For window user:

ipconfig

For Linux and Mac user:

ifconfig

    • 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:

Figure 1

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.

Figure 2

 

After running a command, it will say server is running at port 8000 it is a default port we can also change the port number but it not needed. Than open any web browser and write the ip address:port in a url section it will shared with all of the user.

Figure 3

As shown in figure 3 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 than you have to zip it before sharing which will make it enable to download 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 2 files are necessary to create one is server and other one is client file. Server file is initiated first and listen for the new connection, once connection is established it can send and receive the data. Following is the example of TCP as python code.

Client.py
import socket
sock = socket.socket()
sock.connect(('127.0.0.1',7560))
data = "hello world"
sock.send(data.encode('utf-8'))

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 socket library via pip. In line 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 take 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.

server.py
import  socket
s = socket.socket()
s.bind(('127.0.0.1',7650))
s.listen(5)
print('Server start listening')
while True:
    c,add = s.accept()
    data = c.recv(1024)
    print(data.decode('utf-8'))
c.close()

In the server script, we first import sockets and initialize the instance of socket class. It is to be noted that if there is TCP connection of client file than 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 than 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, socket starts listening on that port. The number 5 here indicated the number of users, means 5 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 next step, if someone requests that means sockets connection request is made the program enter into while loop and accept 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 client file.

 

UDP:

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 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().



Login/Sign Up

Comments




Related Posts



© Copyright 2019, All Rights Reserved. paayi.com

This site uses cookies. By continuing to use this site or clicking "I Agree", you agree to the use of cookies. Read our cookies policy and privacy statement for more information.