Networking in Python

Python Networking : Learn Python Networking in Detail with Real Life Examples
Written by PaayiAdmin |25-Apr-2019 | 0 Comments | 738 Views


Python contains 2 types of networking at a low level and a high level. The low-level network programming is supported by the sockets which allow the user to create a network which then allows you to create a client-server network and performs the functionality of networking. The higher level network programming allows the user to access the specific higher level network protocols like HTTP, FTP, etc.

However, before starting and digging into the python code, we must know what networking is.



Sometimes we do not use only one program to run on a system. The application depends upon other application which either running on the same system as an individual program or running on a different system. So, to connect and communicate between these two separate programs we have to create a network.

These networks allow sending receive and communicate between 2 or more than 2 programs. Here the question arises that if 2 programs are running on the same system then why there is a need for creating a network, we can achieve this by doing multi-threading or multiprocessing?

The answer to this question is number because multi-threading does not allow to run 2 programs concurrently there is a blocking time depending upon how the system spawns two programs, and multiprocessing does not allow to communicate between 2 programs.

In this section, we will learn about the socket programming in python, and we will also learn some command line tricks to create our file sharing server. We will mainly focus on creating a LAN server, but I will let you know how to make your server online by using third-party software



Sockets are the terminal or the endpoints for network programming. Sockets are bidirectional channels for communicating between 2 processes or between 2 system or even between 2 countries. Like if one person is sitting in Pakistan and other is in the UK, he or she can communicate via sockets. A typical example of this program is chat applications. So now the question arises how the sockets send the data. Here come the protocols. At the low level we mainly focus on 2 internet protocols. However, before discussing them, we must know what is meant by protocols.


Internet Protocol:

By protocols, we mean what the rules and regulations on which the data has to be sent on. These rules and regulations are predefined within an internet protocol. There is a list of protocols that are used in our daily life. Like when you click on a link HTTP protocol is used, similarly while sending mail SMTP protocol is used while streaming online video RTSP protocol is used. There are many protocols, but today we will discuss UDP and TCP



TCP stands for Transmission Control Protocol. It is a connection-oriented protocol. It works on handshake rule. It is a way of ensuring that connection has been established between the server and a client and therefore data can be shared safely.

TCP in python

Figure 1


Here are the steps that are involved in creating a TCP server and sending and receiving a message during communication. The TCP sends the acknowledgment at every end of the step to inform whether the data has been sent or not. It will increase the time but ensure the transfer of data.


Properties of TCP:

  • Reliable: Ensure the data transfer.
  • Ordered: Ensure that the message is sent in an order.
  • Heavy Weight: Not just send the message but also attach the additional headers like ACK header and order of packets header which make it heavy and required some time to send.



UDP stands for user datagram protocol. Unlike TCP which is connection-oriented protocol, UDP is a connectionless protocol. UDP does not send any acknowledgment which makes it not reliable but its fast than TCP. The steps involve in UDP can be seen in the diagram:

UDP by Paayi


Figure 2

As shown in the figure after sending the request and connected to the server it can send the response there is no acknowledgment involves in UDP which makes it fast and unreliable at the same time.

  • Light Weight: UDP is fast than TCP
  • Unreliable: No acknowledgment makes it unreliable.
  • Unordered: If we send 4 packets of the message at the same time the server will not recognize the order of packet.


Terminologies in Socket Programming:

  • Hostname: Hostname is the address of the device. An IP address is known as the hostname of the device. The local IP address of any device is if someone is creating a network within the same system so user can use as a hostname.
  • Port: Suppose if a hostname is the address of a hotel than a port number is its room number. The full address is needed for reaching the destination. Many servers are running on different ports, so it is necessary to allocate the free port to the network.


Functions in Socket Programming:

  • socket.bind(): This function bind the hostname(IP address ) of the sender and port at which the network is currently running.
  • socket.listen(): This function starts listening at the address. It takes the argument in an integer the greater the integer the maximum number of senders can send the request to the network.
  • socket.accept(): This function accepts the request. It only works in TCP as UDP don’t need acknowledgment. It is the tuple of connection and address, and then it receives the data.
  • socket.connection(): This function establishes the connection at the client end. It is also working in TCP only.
  • socket.recv(): In TCP this function receives the message.
  • socket.send(): In TCP it sends the data. It is to be noted that it can only send the data in the form of bytes, not as a string.
  • socket.recvfrom: In UDP this function sends the message.
  • socket.sendto: In UDP it sends the message. It is to be noted that normally we can only send the data of 514 bytes in UDP. Else we have to make packets.
  • socket.close(): this will close the connection.


This is the detailed introduction of socket programming. In next section we will learn about the programming and how to deal with it.

Login/Sign Up


Related Posts

© Copyright 2020, All Rights Reserved.

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.