Python contains two 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 two programs.
Here the question arises that if two programs are running on the same system, then why is there a need for creating a network? Can we achieve this by doing multi-threading or multiprocessing?
The answer to this question is number because multi-threading does not allow us to run two 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 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 systems, or even between 2 countries. Like if one person is sitting in Pakistan and the 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 a low level, we mainly focus on two internet protocols. However, before discussing them, we must know what is meant by protocols.
By protocols, we mean 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, the 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.
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 a 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:
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 four packets of the message at the same time, the server will not recognize the order of the 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 127.0.0.1 if someone is creating a network within the same system so the user can use 127.0.0.1 as a hostname.
- Port: Suppose if a hostname is the address of a hotel, then a port number is its room number. The full address is needed to reach 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 to the address. It takes the argument is 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 doesn’t need acknowledgment. It is the tuple of connection and address, and when 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 the next section, we will learn about programming and how to deal with it.