Javatpoint Logo
Javatpoint Logo

Socket Programming Using Python

Python is the most versatile language and it has a vast range of library which used to almost every trending fields. As we know that, Python has easy syntax and user-friendly environment that makes development or data analysis straightforward.

Python is also compatible for the network programming and it consists of many libraries that offer higher-level access to specific application-level network protocols, such as HTTP, FTP, and so on. First we need to understand the basic terminologies of the networking.

Improvements in socket programming using Python in recent years have concentrated on enhancing performance, security, and scalability. Incorporating asynchronous frameworks and libraries, such as asyncio and aiohttp, has made it possible to program networks concurrently and effectively.

Python programmers increasingly use asynchronous programming because it enables them to create concurrent, non-blocking code that effectively manages numerous connections. A foundation for asynchronous programming is provided by the asyncio package in Python, which enables the development of event loops and coroutines. Asyncio allows processing numerous connections without requiring threads or processes in socket programming, improving performance and resource utilization.

Security has grown to be an essential component of socket programming in addition to speed improvements. Python offers several modules and protocols for secure network connection, including TLS( Transport Layer Security) and SSL (Secure Socket Layer). These encryption technologies shield data transported over the network from unauthorized access and manipulation. Protecting sensitive information and preserving the integrity of networked systems are made easier by including secure communication protocols in socket programming using Python.

Additionally, scalability has been a key factor in contemporary socket programming. Commonly used Python web development frameworks like Flask and Django offer built-in functionality for managing network requests and answers. These frameworks handle the complexity of socket programming, allowing programmers to concentrate on creating scalable web applications with features like load balancing, caching, and distributed computing.

What is Computer Network ?

Computer networks allow resource sharing and communication between several computer systems or devices. The infrastructure that connects these devices and allows for exchanging information between them is called a computer network. To facilitate communication and cooperation, a network connection must be established.

Let's use making a phone call as an example to comprehend computer networks better. A network service provider must create a link between our device and the recipient's device when we make a phone call. Similar requirements apply to computer networks where devices must be linked together to exchange data and have access to shared resources.

Computer networks may be divided into several categories based on their size and coverage area. Typical network types include the following:

  • Local Area Network (LAN): A LAN is a network that covers a restricted area of land, such as a campus, office building, or other similar structures. It links devices within a constrained space, enabling quick and effective communication.
  • Wide region Network (WAN): A WAN links several LANs and often spans a greater geographic region. It makes it possible for gadgets to communicate with each other across towns, nations, and even continents. An expansive, worldwide WAN is exemplified by the Internet.
  • Metropolitan Area Network (MAN): Regarding geographic coverage, a MAN sits between a LAN and a WAN. It links devices throughout a city or metropolitan region, enabling more extensive resource sharing and communication.
  • PAN: PAN (Personal Area Network) is a network that connects devices within an individual's personal space, such as a smartphone, smartwatch, and computer.
  • Wireless Networks: Wireless networks build connections without physical wires using wireless communication technologies like Wi-Fi, Bluetooth, or cellular networks. They enable devices to connect and communicate with flexibility and mobility.

To operate efficiently, computer networks need various hardware and software components. These consist of the following:

  • NIC: Network interface cards (NICs) are physical objects used to connect devices to a network. They give network-connected devices a physical interface to send and receive data.
  • Routers and switches: Routers and switches are networking tools that aid in rerouting and directing network traffic. Switches enable communication inside a network by routing data to the correct receiver, whereas routers link several networks and permit data transmission between them.
  • Protocols: The rules and norms for communication between devices on a network are defined by protocols or network standards. Common protocols include HTTP (Hypertext Transfer Protocol), which is used for online communication, and TCP/IP (Transmission Control Protocol/Internet Protocol), the Internet's founding protocol.
  • Network Security: To safeguard sensitive data and prevent unauthorized access, it is essential to ensure the security of computer networks. Virtual private networks (VPNs), firewalls, encryption, and authentication techniques are examples of network security methods.

Computer networks continue to develop as technology does. By linking an increasing number of devices and allowing quicker and more reliable communication, emerging technologies like the Internet of Things (IoT) and 5G are altering networks.

LAN

A form of computer network known as a local area network (LAN) links a very small number of computers within a constrained geographic region. It is frequently utilised in places including homes, workplaces, small businesses, and institutions. The devices in a LAN are often interconnected peer-to-peer, enabling direct communication and resource sharing between them.

From a tiny network with a single user to a corporate network with many users and devices, the size of a LAN can vary. To link devices in a small space and enable smooth communication and collaboration, LANs offer a practical and affordable option.

As networking technology have advanced, LANs have seen tremendous change. Nowadays, wireless LANs (WLANs) are more and more common because they provide wireless connectivity flexibility inside a constrained region. Wi-Fi technology is used by WLANs to allow devices to connect to the network without the use of physical connections.

WAN

Multiple Local Region Networks (LANs) are connected by a Wide Area Network (WAN), which spans a large geographic region. The internet is an example and makes communication possible across great distances. Data is split into smaller packets for independent transmission when packet switching on WANs. This approach enables concurrent data streams and optimal network resource utilization.

WANs are essential for businesses with scattered locations because they allow for smooth communication and information exchange between branch offices. They also play a crucial part in distributed systems and cloud computing by facilitating access to distant resources. Performance is enhanced via WAN optimization methods like data compression. WAN bandwidth and speed have increased due to innovations like fiber optics and 5G networks. Software-defined WAN (SD-WAN) solutions increase flexibility and centralize management. To address the escalating demands of global connection, WANs continue to develop.

MAN

A network called a Metropolitan region Network (MAN) is smaller in scope than a Wide Area Network (WAN) but covers a wider geographic region than a Local Area Network (LAN). It connects computer users and resources across a large geographic area, usually in a city or metropolitan area.

A MAN allows for integrating numerous LANs or other networking technologies within the defined metropolitan area, extending communication beyond the confines of a single LAN. It offers a way for businesses or institutions nearby to share resources, exchange information, and improve communication.

The network infrastructure that links several buildings, universities, or workplaces inside a city is a classic illustration of a MAN. A MAN provides effective resource sharing and communication among linked sites by building a single, broader network. It is appropriate for applications that need more widespread connection within a particular urban or suburban region since it provides more bandwidth and wider coverage than a LAN.

PAN

Personal digital assistants (PDAs), PCs, cellphones, and tablets may all communicate over a personal area network (PAN). A PAN's main function is facilitating data sharing and communication among various personal gadgets.

PANs may be used for many different things, including joining devices to other devices or joining them to a higher-level network. For instance, a PAN may create a wireless link between a laptop and a smartphone to share data and connect to the Internet. Additionally, it may be used to link devices to a larger network, such as the Internet or a LAN.

To create PANs, Bluetooth technology is frequently employed. Bluetooth enables short-range wireless communication between devices, usually around 10 meters apart. Bluetooth-enabled devices may connect and pair with one another to create a PAN.

PANs allow users to connect to and communicate with their own devices conveniently and flexibly. Within the constrained PAN range, they make smooth data transfer, file sharing, and device synchronization possible. PANs are particularly helpful when several personal devices must communicate with one another or join a larger network.

Wireless Networks

Wireless networks are known as wireless networks that convey data and create connections between devices using wireless communication technologies since no physical cables or wired connections are required. These networks allow devices to connect and exchange data wirelessly, bringing flexibility, portability, and convenience in various scenarios.

Data transmission between devices happens through wireless networks using radio waves, infrared signals, and other wireless communication methods. Wi-Fi is the most popular form of wireless networking technology. Wi-Fi enables devices to join a local wireless network, often supplied by a wireless router, and connect to it to access the internet or communicate with other devices on the network. Wi-Fi networks are now present in homes, businesses, public areas, and other settings, offering wireless access for various devices, including smartphones, laptops, tablets, and IoT devices.

Bluetooth is another widely used wireless technology, which is particularly made for close-proximity communication between devices. Bluetooth facilitates wireless connections between nearby devices, allowing you to connect your smartphone to wireless headphones or move files between them.

Another wireless network that offers internet access and mobile communication to devices across a large geographic region is the cellular network, which includes the 3G, 4G, and 5G networks. Cellular networks need a network of base stations or cell towers to transmit and receive signals between devices and the network infrastructure. This enables users to make phone calls, send text messages, and access the internet while in motion.

Computer Network Terminologies

These terminologies are very essential for the network programming. If you are not familiar with them then understand them for further tutorial.

Internet Protocol

Data transmission and reception over the Internet are governed by a set of rules and procedures known as Internet Protocol (IP). It is a cornerstone of the Internet protocol family and the basis of online communication.

IP assigns each connected device a different IP address to provide a system for unique identification. IP addresses are numbers that enable the identification and location of devices on a network. IPv4 (Internet Protocol version 4) and IPv6 (Internet Protocol version 6) are the two IP versions currently in use. IPv6 addresses are expressed as a series of eight groups of four hexadecimal digits separated by colons, as opposed to IPv4 addresses, which comprise a sequence of four numbers separated by periods.

For example - There are two friends A and B. Suppose A want to communicate to the B. Then, he wrote a letter and went to the Post-Office (A kind of a communication network). He putted the letter into envelop and submit it in the Post Office for delivery to B. He attached the address of B so that the letter can be reached at the correct destination.

Now, A wants to send the script of 15 pages to B. So, there is a chance that one envelop cannot fit in a single envelop. So he decide to put each page in separate envelop. Now, there is chance the post office will not deliver the letter in the same order as they were supposed to be? Here the internet protocol plays important role.

Data packet routing across networks is handled by IP. When sending data over the internet, it is broken up into smaller packets with the essential addressing information and a part of the original message. These packets are then sent out individually, and the paths they travel to get there might vary. IP uses IP addresses to route packets across networks and handles packet fragmentation and reassembly to guarantee that each packet is delivered to the proper destination.

  • UDP(User Datagram Protocol)
  • TCP(Transmission Control Protocol)

User Datagram Protocol

UDP also has the feature of being unordered. Datagrams may only sometimes be received by their recipients in the same sequence they were transmitted. UDP treats Each datagram as a separate entity, not imposing any particular packet sequence or order. This quality might be helpful in situations when timely data transmission is more important than exact ordering.

Because a connection does not need to be established before data delivery, UDP is seen as lightweight. UDP runs on a best-effort basis without the expense of connection management, in contrast to TCP, which requires a handshake process to establish a dependable connection. UDP is more effective in resource utilization and processing overhead due to its lightweight nature. Data packet routing across networks is handled by IP. When sending data over the internet, it is broken up into smaller packets with the essential addressing information and a part of the original message. These packets are then sent out individually, and the paths they travel to get there might vary. IP uses IP addresses to route packets across networks and handles packet fragmentation and reassembly to guarantee that each packet is delivered to the proper destination.

The data packets that UDP uses are called datagrams. The information required for transmission and integrity verification is handled as a distinct entity in each datagram. These datagrams are transmitted one at a time and may follow several routes to their destination. UDP does not provide retransmission or error correction techniques. Therefore the application layer must ensure data integrity and dependability as needed.

Transmission Control Protocol

A connection-oriented protocol called TCP (delivery Control Protocol) enables dependable and well-organized data delivery over the Internet. It works by establishing a connection with another host before starting a data transmission based on the handshake idea.

When one host says "Hello," and the other responds with "Hello," the connection has been established. This is the handshake procedure in TCP. Both hosts must complete this handshake to share data across the network.

TCP possesses several properties that differentiate it from UDP:

  • Reliability: Because TCP has procedures for message acknowledgment, retransmission of missing packets, and timeout management, it is more dependable than UDP. It ensures the data is transmitted successfully and allows for repeated transmission attempts if necessary.
  • Ordered: TCP ensures that messages are transmitted in the correct sequence. The messages are sent to the recipient in the same sequence that they were sent. This guarantees accurate data reconstruction at the receiving end.
  • Heavyweight: When compared to UDP, TCP is somewhat heavyweight. Before any user data is transmitted, a socket connection must be established by a three-packet procedure known as a three-way handshake. SYN (synchronization), SYN+ACK (acknowledgment), and ACK (acknowledgment) are the three packets in question. This handshake process adds some overhead to the communication but ensures a reliable connection.

TCP is frequently employed for applications like web surfing, file transfers, and email that need dependable and organized data transmission. It maintains data integrity and completeness through methods like error detection, flow control, and congestion control.

IP Addresses and Ports

A network, including the internet, assigns devices linked to its IP addresses, which are distinctive numerical identities. They act as the location and identification information for gadgets like computers, servers, and IoT devices. To create a connection between devices online, IP addresses are necessary.

Ports, on the other hand, are operating system endpoints that provide communication between applications or services. A port's numerical identification is used to identify between various services or processes operating on a device. On a device, ports enable the simultaneous use of numerous programs and make transferring data between them easier.

Example -

In the previous example, A wants to send letter to B. So A requires to know the address of B to deliver the package successfully. Now, A has the unique postal address of B and hence the postman delivers the letter successfully. So IP address is like the postal Address.

A system might have thousand running services but we can uniquely identify a service using port number. There are total of 0-65535 ports on a system.

You may compare an IP address to the address of a building and a port to the room number inside that building as an illustration. The device is identified by its IP address, and the port identifies the program or service operating on it.

IP addresses and ports operate together to provide precise communication between networked devices and applications. Data is delivered to a specific IP address and sent to a specific port on the target device connected to a particular application or service when transferred over the internet. The targeted application or service may then correctly receive and handle the data.

Sometime the Port number can be seen in web or other uniform resource locators (URL) as well. By default, HTTP uses port 80 HTTPS port 443. Below is the example of the common ports.

Port Number Description
22 Secure Shell
23 Telnet Remote Login Service
25 Simple Mail Transfer Protocol (SMTP)
53 Domain Name System (DNS) Service
80 Hyper Text Transfer Protocol (HTTP) which used in WWW.

There are two types of the IP addresses.

Private IP Address: Local networks, such as those in a house or business, employ private IP addresses inaccessible from the internet. Three private IP address ranges are frequently employed:

  1. 168.0.0 - 192.168.255.255
  2. 16.0.0 - 172.31.255.255
  3. 0.0.0 - 10.255.255.255

Thanks to these private IP addresses, devices within a local network can connect without directly using the internet. They are frequently used for local area networks (LANs), home networks, and small companies.

Public IP Address: On the other hand, an internet service provider (ISP) gives a router or gateway equipment connected to the internet a public IP address. It is distinct and globally routable, enabling communication with the outside world for devices linked to the internet. For devices to be reachable via the internet and to be able to access websites and online services, public IP addresses are necessary.

ISPs offer public IP addresses, which can be dynamic (subject to change) or static (permanent). Static public IP addresses are frequently used for services that need constant accessibility, such as web servers or remote network access. In contrast, dynamic public IP addresses may change regularly.

Firewall

A firewall is a barrier between an internal network and external sources, such as the Internet, and is essential to network security. Its main duties include monitoring incoming and outgoing network traffic and enforcing a predetermined set of security rules. A firewall evaluates the data packets and decides whether to allow or stop the traffic following the defined rules.

A firewall's job is to defend against internal network risks such as malicious assaults, unauthorized access, and data breaches. It performs the filter function by looking at each data packet's source and destination addresses, port numbers, and other pertinent data. The only packets allowed to travel through are those that fulfill the preset security standards, whereas suspicious or possibly harmful packets are blocked.

For example

Suppose the IP addresses as houses and port numbers as room within the house. Only authenticate people (source address) are allowed to enter the house (destination address).

Why Python for Networking Programming?

Python, a powerful and adaptable programming language, has become incredibly popular for network programming. There are a few justifications for why Python is a favored decision for network programming, and in this clarification, we will investigate a portion of the key variables.

First, Python is an excellent language for network programming due to its readability and simplicity. Its perfect and natural punctuation permits engineers to compose code that is straightforward and keep up with. Because of its simplicity, Python code is less likely to have bugs because it is less likely to contain errors. Additionally, Python's extensive standard library offers a comprehensive collection of networking-specific modules and functions, making the implementation of network protocols and communication straightforward.

Also, Python's cross-stage similarity goes with it a phenomenal decision for network programming. Without requiring significant modifications, Python programs can run on Windows, macOS, and Linux operating systems. Because networks typically contain heterogeneous devices and systems, this flexibility is essential in network programming. Because Python is portable, it makes it simple to install and run network applications on a variety of platforms. This makes it easier to develop and maintain the applications.

Python's extensive ecosystem and community support are yet another significant advantage. There are a lot of third-party libraries and frameworks available for Python that can really help with network programming tasks. High-level abstractions for network communication are provided by libraries like "socket" and "asyncio," making it simpler to handle sockets and implement networking protocols. Systems like 'Curved' and 'Django' offer extensive instruments and parts for building complex organization applications, including web servers and APIs. Developers can use pre-existing solutions to accelerate development and reduce overall effort thanks to these resources' availability.

Additionally, Python's adaptability enables it to seamlessly integrate with various technologies and languages. It allows network applications to interact with components written in different languages thanks to its support for a variety of inter-language communication mechanisms like APIs, sockets, and message queues. When existing network infrastructure or tools are written in languages other than Python, this capability is especially useful. Python's ability to work with other technologies makes it easier to integrate and work together seamlessly, which makes network programming tasks more effective as a whole.

Lastly, Python's popularity for network programming is bolstered by its extensive documentation and large community. Numerous online resources, tutorials, and forums for developers to seek guidance and share knowledge are available to the vibrant and active Python community. Developers can easily access information and find solutions to common networking issues thanks to the extensive documentation.

Basic of Socket Programming

We have learned the basic concepts of the network and understand the basic network terminology. Before starting the Python network programming, we should go through the socket introduction.

The networking and sockets programming are the huge subjects. Computer network has many topics to explore but here we discuss the basic network programming using Python.

Let's understand the socket and see what they are and why we use them.

What is Socket?

First, we need to learn the internet connection. The internet connection is basically used for connect two end points across the internet for data sharing and other things.

A socket is a software interface that enables two computers to communicate with one another across a network, such as the Internet. It is an endpoint for data transmission and reception between programs on various devices. The internet connection provides the facility to the one process from computer C1 can communicate to a process from a computer C2. It consists of following features.

Reliable: A dependable method of data transport via an internet connection is provided by sockets. They provide accurate and error-free data transmission while establishing a secure communication path between the client and server.

Point to Point: Sockets connect two particular endpoints in a point-to-point fashion. Data can be transferred and received at the desired location since its IP address and port number can recognize each endpoint.

Full Duplex: Communication that is full-duplex is one of sockets' primary characteristics. This implies that data can be transferred concurrently between the client and server in both ways. Data can be sent and received by both the client and the server, enabling interactive and bidirectional communication.

The endpoints for two-way, point-to-point communication channels are provided by sockets, which are essential for network communication. They are frequently employed for message transport via networks. Two endpoints are involved when a client, like a web browser, connects to a server, like www.javatpoint.com: the client socket and the server socket.

The idea of sockets first appeared in 1971 and later developed into the Berkeley socket Application Programming Interface (API), included in the Berkeley Software Distribution (BSD) operating system in 1983.

Client-server applications became extensively utilized in 1990 with the launch of the Internet and the World Wide Web, which boosted socket utilization. One side serves as the server in client-server applications, waiting for client connections. This approach permits numerous networked services, including web servers, email servers, and database servers, and effectively manages many customers.

Sockets enable communication between clients and servers using several protocols, including the User Datagram Protocol (UDP) and the Transmission Control Protocol (TCP). Thanks to the dependable, connection-oriented TCP protocol, data packets will always be sent in the proper order. It creates a link between the client and server, guaranteeing the accuracy and dependability of data transfer. Conversely, UDP is a lightweight, connectionless protocol that puts speed above dependability. Low latency is crucial for real-time applications, where it is often employed.

No particular programming language is required for socket programming. It is compatible with several languages, including Python, Java, C++, and C#, making it available to developers working on multiple platforms.

To enable new technologies, socket programming has advanced recently. For instance, the use of IPv6 has improved connection and addressing to accommodate the increasing number of devices connected to the Internet.

Socket API Functions

Application programmers may construct, configure, and manage sockets in their network applications using the Socket API (Application Programming Interface), which offers a collection of functions. These activities include creating sockets, binding to particular addresses, creating connections, exchanging data, and shutting sockets, among others. Following are a few frequently used Socket API functions:

  1. socket(): A new socket is created using the socket() method, providing a socket descriptor that may be utilized for later actions. It accepts arguments that indicate the socket type (such as SOCK_STREAM for TCP or SOCK_DGRAM for UDP) and the address family (such as AF_INET for IPv4 or AF_INET6 for IPv6).
  2. bind(): Using the bind() method, a socket may be bound to a particular IP address or port on the network. The IP on which the server will listen for inbound connections is commonly specified on the server side.
  3. listen(): A socket can accept incoming connections by being marked as a passive socket using the method listen(). The socket's maximum capacity for pending connections is specified.
  4. accept(): Using the accept() method, a listening socket can accept incoming connection requests. For the accepted connection, it generates a fresh socket and returns a new socket descriptor. On the server side, this function is commonly employed.
  5. connect(): This method starts a client socket connection to a distant server. It creates a connection using the address of the distant server.
  6. send() and recv(): These two functions-send() and recv()-are used to send and receive data across a socket. They give the client and server the means to send and receive messages. These operations provide timely and trustworthy data transmission for connection-oriented sockets like TCP.
  7. sendto() and recvfrom(): In contrast to send() and recv(), sendto() and recvfrom() are used with connectionless sockets, such as UDP. They enable data transmitting and receiving without first establishing a connection.
  8. close(): Using the close() method, a socket is closed, and all related resources are released. For connected sockets, it breaks the connection and stops any further communication.
  9. getaddrinfo(): Based on a hostname, port, and other factors, this function is used to compile a list of appropriate network addresses. It offers both IPv4 and IPv6 network address resolution flexibility.
  10. setsockopt(): This method enables socket option modification. It allows for precise management of several socket properties, including setting the socket to reuse addresses, changing socket buffer sizes, and activating multicast options.
  11. getsockopt(): Gets the current value of a socket option using the getsockopt() method. It enables requesting information about the current configurations or states of socket options, such as finding out the timeout value or the value of a certain socket flag.
  12. shutdown(): With the help of the shutdown() method, a socket connection may be gracefully terminated. It can terminate a connection's transmitting or receiving end or both. Before shutting the socket, it checks that all outstanding data has been transferred.
  13. select(): This function enables waiting until at least one socket is prepared for reading, writing, or unusual circumstances before monitoring several sockets for activity. It helps manage numerous sockets effectively and simultaneously.
  14. gethostbyname() and gethostbyaddr(): DNS (Domain Name System) resolution uses these functions. They either convert IP addresses to hostnames (gethostbyname()) or hostnames to IP addresses (gethostbyaddr()).
  15. inet_ntop() and inet_pton(): These two functions, inet_ntop() and inet_pton(), make it easier to translate human-readable IP addresses from string format to binary form. When using IP addresses in network programming, they are useful.
  16. fcntl(): This function offers a variety of operations for working with file descriptors, such as setting their flags or switching their non-blocking mode.

Creating a Socket

When we clicked on the link that brought us to a particular page. The web-browser acts as the following ways.

Client.py

The above code will work on the client side. When the client tries to communicate the server, an Ephermal Port is assigned by the operating system for the connection. An Epherimal Port is nothing but a random port which is assigned by the operating system. The client socket closes as soon as data exchange is complete. The client socket is only used for the one time.

The server creates a server socket which is slightly complex than client socket. Let's see what happen at server side.

Server.py

Difference between Client and Server Socket:

Client Socket

  • To connect to a server, client programs employ client sockets.
  • They start a conversation by making queries to the server. Then they get replies.
  • Client connections are temporary and made for certain interactions or activities.
  • Ephemeral ports, which the operating system dynamically assigns, are often used by them for communication.
  • Data sent to the server and received must be processed by client sockets.

Server Socket:

  • Applications running on servers employ server sockets to check for new client connections.
  • They wait for connections from clients and manage several client connections simultaneously.
  • Server sockets have a long lifespan and are intended to be always available.
  • They operate by listening on specified ports linked to particular services or protocols.
  • Server sockets are in charge of receiving incoming client connections and responding with services or data.
  • They can frequently manage several client connections at once by setting up different threads or processes for every connection.

Python Socket Module:

The Python socket module provides the methods required for generating and using sockets. Socket.socket() is the primary technique for creating sockets. The syntax for the socket() function is as follows:

The socket() method's arguments are as follows:

  • socket_family: The address family for the socket is specified by socket_family. It can be either AF_UNIX (for sockets in the Unix domain) or AF_INET (in the Internet domain).
  • socket_type: the socket's type is specified by socket_type. For TCP sockets, it can be SOCK_STREAM, and for UDP sockets, it can be SOCK_DGRAM.
  • protocol: specifies the particular protocol to be used. It is normally set to 0, enabling the operating system to select the proper protocol following the provided socket_type.

In most cases, we will work with AF_INET sockets (for the Internet domain) and SOCK_STREAM sockets (for TCP). Once the socket is created, we can use various methods provided by the socket module to interact with the socket, such as bind(), connect(), listen(), send(), recv(), and so on.

These methods allow us to bind the socket to a specific address and port, establish a connection to a remote server, listen for incoming connections, send data, receive data, and perform other socket-related operations.

In the client-server socket model, the client socket connects to the server while the server socket waits for incoming connections. While the client socket utilizes the connect() method to create a connection, the server socket sets up the socket using the bind() and listen() methods.

When creating network applications that utilize socket communication, it is essential to comprehend and use the Python socket module's functions.

Client Sockets Methods

The client socket method is given below.

connect()

This function is used to set up a connect to a remote socket at an address. An address format contains the host and port pair which is used for AF_INET address family.

Server Socket Methods

The server socket methods are given below.

bind()

This method is used to bind the socket to an address. The address's format depends on socket family mentioned above (AF_INET).

listen(backlog)

This method is used to listen for the connection made to the socket. The backlog is a term which used to represent the maximum number of the queued connection that must be listened before rejecting the connection.

accepts()

The accepts() method accepts a connection. The socket should be bound to an address and ready to listen the connections. It returns the pair(conn, address) where con is a new socket object which can be used to send and receive data on the connection, and address is the address linked to the socket on the other end of the connections.

Few Common Socket Methods

A few commonly used functions for server object is given below.


TCP Socket Methods UDP Socket Methods
Server_Object.recv() - Receives TCP messages Server_Object.recvfrom() - Receive UDP messages.
Server_Object.send() - Transmit TCP messages Server_Object.sendto() - Transmits UDP messages.

Types of Sockets

In networking, there are two different kinds of sockets: SOCK_STREAM and SOCK_DGRAM. Let's examine each kind in more detail:

SOCK_STREAM:

  • TCP (Transmission Control Protocol)-based communication uses SOCK_STREAM sockets.
  • They offer a dependable stream of data that is connection-focused.
  • Data is transferred via SOCK_STREAM in a continuous stream, guaranteeing that it will arrive in the same order as it was sent.
  • These sockets ensure data delivery and automatically manage any retransmissions or missed packets.
  • Applications that demand dependable and organized data delivery, such as web surfing, file transfers, email, and real-time communication protocols like HTTP, FTP, and SSH, frequently employ SOCK_STREAM sockets.
  • Data may be delivered and received in both directions since the communication is bidirectional.

SOCK_DGRAM:

  • For UDP (User Datagram Protocol)-based communication, SOCK_DGRAM sockets are utilized.
  • They provide an unstable, connectionless datagram service.
  • Data is transmitted via SOCK_DGRAM in separate packets known as datagrams, each of which is a distinct communication unit.
  • Datagrams may be lost or duplicated during transmission, and there is no assurance that they will be received in the order they were sent.
  • Applications like real-time multimedia streaming, online gaming, DNS, and SNMP that can tolerate the odd packet loss or out-of-order delivery frequently employ SOCK_DGRAM sockets.
  • Data may only be sent in one direction during unidirectional communication.

TCP Sockets

In this section, we will create the socket object using socket.socket() function and specify the socket type as socket.SOCK_STREAM. As we know that, A protocol is essential to send data one end to another. Here the TCP (Transmission Control Protocol) is used default protocol. It is highly operative and reliable. We can consider this protocol because -

  • It is reliable - If the packet is missed in between the transfer to the other end then it retransmitted by the sender.
  • In order data deliver - The message is send in the same order as it written by the sender.

On the other side, User Datagram Protocol (UDP) sockets make with socket.SOCK_DGRAM isn't reliable. The data read by the receiver can be same order as sender's writes.

Let's understand the following example of simple Client-Server Program.

Simple Server Program

Example -

The above code will not do anything as of now. It waits for a client to connect at the specified port. It will give the following output, if there is no error in the program.

Similarly, every site that we visit consists of a server on which it is hosted.

Now, we will create a client.py program to connect with the server.py file.

Simple Client Program

Consider the following client.py program. The client tries to set the connection to server's port; we are assigning the 9999 well well-defined port.

Example -

To get the result, first run the server.py file and then run client.py file. If there is no error then, it will give the following output.

Note - Here, the client and server files are running on the same machine, but in the real life server is situated on the different place. The point is to be notice here the client.py is terminated but the server.py is still running. This is also happen in real life scenario.

For example - When you make request to the javatpoint.com then it fulfill the request and its server continuously running (24*7) in the background.

Python Internet Module

The following are the list of the Python's module related to the network programming.

Protocol Common function Port No Python module
HTTP Web pages 80 httplib, urllib, xmlrpclib
NNTP Usenet news 119 nntplib
FTP File transfers 20 ftplib, urllib
SMTP Sending email 25 smtplib
POP3 Fetching email 110 poplib
IMAP4 Fetching email 143 imaplib
Telnet Command lines 23 telnetlib
Gopher Document transfers 70 gopherlib, urllib

The table lists common network protocols, their associated functions, default port numbers, and the corresponding Python modules that can be used for network programming. Let's explain each entry:

1. HTTP (Hypertext Transfer Protocol):

Used for retrieving and transmitting web pages and related files.

Default port number: 80.

Python modules: httplib, urllib, xmlrpclib.

2. NNTP (Network News Transfer Protocol):

Used for accessing and posting Usenet news articles.

Default port number: 119.

Python module: nntplib.

3. FTP (File Transfer Protocol):

Used for transferring files between hosts on a network.

Default port number: 20 (data transfer), 21 (control).

Python modules: ftplib, urllib.

4. SMTP (Simple Mail Transfer Protocol):

Used for sending email messages between servers.

Default port number: 25.

Python module: smtplib.

5. POP3 (Post Office Protocol version 3):

Used for retrieving email from a remote server.

Default port number: 110.

Python module: poplib.

6. IMAP4 (Internet Message Access Protocol version 4):

Used for retrieving email from a remote server, with more advanced features than POP3.

Default port number: 143.

Python module: imaplib.

7. Telnet:

Used for establishing remote command line connections.

Default port number: 23.

Python module: telnetlib.

8. Gopher:

Used for retrieving documents from remote servers.

Default port number: 70.

Python modules: gopherlib, urllib.

These Python modules offer practical classes and methods for interacting with the relevant network protocols. They enable programmers to perform network-related operations using Python, including performing HTTP requests, retrieving emails, moving files, and more.

Working with UDP Sockets

We have learned that, if we don't mention the socket family and socket type then by default it is TCP. To create the UDP socket, we need to specify the socket family and socket type explicitly. Let's understand the following syntax.

Syntax -

Let's understand the following UDP server program.

UDP Server Program

Write the below script and save it named server.py

Example -

Output:

Waiting for client

UDP Client Program

Save this file named udpclient.py

Example -

Output:

Waiting for client.... 
Received Messages: Welcome To JavaTpoint from('192.168.43.217,5342')
Waiting for client

We have discussed both sockets and establish the connection between the client-server sockets.

Echo Client and Server

A client can submit a message or request to a server using an echo server, a straightforward network application, and the server will respond by sending the identical message or request back to the client. A fundamental client-server communication paradigm is shown.

The echo server works in the following ways:

  • The server monitors a certain network port for incoming client connections.
  • A socket is formed for communication between the client and the server when a client connects to the server.
  • The client communicates with or requests something from the server through the socket.
  • The message is delivered from the client to the server.
  • The server processes the message received (which, in the case of an echo server, may not entail any processing at all).
  • Through the socket, the server communicates the same message or response to the client.
  • The client receives the server's echoed message or answer.
  • The client then can send more messages or cut off communication with the server.

Since it enables the client to confirm that the server is successfully receiving and processing messages, the echo server is frequently used for testing and debugging.

The socket module in Python may be used to create an echo server and client. When a message arrives, the server code waits for incoming connections and echoes the message to the client. A connection is established with the server, a message is sent, and the client code shows the echoed answer.

Working with Input and Output Devices

Input and output streams are needed to manage the data flow between an echo server and a client to establish communication. These streams enable communication between the client and server, with the client receiving an echo of the message from the server.

The input stream represents the data flow from the client to the server. The server uses it to receive the message that the client has sent. The server processes the data after reading it from the input stream.

The output stream represents the data flow from the server to the client. The server publishes the same message or a reply to the output stream after receiving the message from the client. The client can then read the echoed message or answer from the output stream.

These input and output streams allow the client and server to communicate in both directions and exchange data. The server may receive Multiple client messages, which may then repeat each one back separately.

Let's understand the following example:

Echo Server

Explanation -

The socket.socket() function creates a socket object that supports the context manager type, we can also use it in a with statement and don't need to call close() function.

We have mentioned the address family and socket type.AF_INET that is an Internet address family for IPv4. The SOCK_STREAM is the socket type of TCP protocol to transport our message in the network.

We have used the bind() function that used to connect the socket with the explicit network interface and port number.

Here, the host_name represents the hostname, IP address, or empty string. The host must be IPv4-formatted address string.

The port number be an integer number which lies in between 1 to 65535 (0 is reserved). It denotes the TCP port number to accept connection from clients. Some system may require the superuser authentication if port is < 1024.

Next, we have used the listen() function, which enables a server to accept() connections.

By default, the listen() function consists of backlog parameter. The backlog parameter represents the number of rejected connection that system will allows before declining new connection.

The backlog helps to manage the maximum length of the length of the queue for pending connection.

When a client establish the connection, it returns a new socket object using the accept() function.

Now, we have a socket object from accept() function. It is important to recall that the socket that we will use to communicate must be distinct from the listening socket that the server is using to accept new connections.

Once we complete the process to get the conn object from accept() function. We used the infinite while loop over blocking calls to conn.recv(). The client sends and echoes it back using conn.sendall().

Echo Client

We are describing the echo client program and save it echo-client.py:

Program -

Explanation:

The echo client is much simple in comparison of echo server. It links a socket object, connects to the server and calls s.sendall() to send messages. The s.recv() to read the server reply and we can print it.

Blocking and Non-Blocking Sockets

In the previous sections, we learned the client sends the request to the server and server process the request; send back a response using the sockets (TCP/UDP).

Blocking Socket I/O

TCP sockets are often configured to run in blocking mode by default when dealing with them. In blocking mode, the program's execution is suspended until a certain operation is finished. For instance, the program will stall and wait for the connection to be formed when executing the connect() function to connect to a server.

In some situations, such as when we want to ensure an operation is finished before going on to the next one, blocking I/O might be helpful. But if the procedure takes longer than anticipated, it could cause delays in the program's execution.

In some circumstances, we may add a system to handle or interrupt active connections while they are in use. Non-blocking mode is useful in this situation. We can allow the program to continue running while waiting for specific activities to finish by setting a socket to non-blocking mode. This gives the socket operations more latitude and control.

Non - Blocking Socket I/O

To toggle between blocking and non-blocking modes for a socket in Python, use the setblocking() function. We may add extra logic to handle disruptions or carry out other activities while waiting for socket operations to complete by switching a socket to non-blocking mode.

The non-blocking mode presents the requirement for adequate error checking and management of exceptions linked to non-blocking I/O operations. Hence it is vital to emphasize that it requires cautious treatment.

Let's understand the following example of Blocking Socket.

Save file blockclient.py

Program

Now, understand the following blockserver.py:

Program

First, run the blockServer.py file and then blockClient.py. The server will continuously printing the message Hello JavaTpoint. This will keep on until all the data is sent. In the above code, the client will not be printed the Hello JavaTpoint for long time. It will take much time due to client has to send the large number of string. It will happen until the socket input/output will get blocked.

The send() function is used to convey the data all over the server, while the write buffer will get complete. The kernel will hold the process to the sleep until the data in the buffer is transported to the client and the buffer is empty again. Once the buffer is empty, the kernel will start its process up again to get the next chunk of data that is to be transported.

Now consider a Non - Blocking Socket.

Program

When we run the non_block_client.py, the program will run for a small time, it will print the "All Data Received" and quickly finished.

If we create the socket non-blocking by the setblocking(0), It will never wait for the operation to complete. So when we call the send() function it will try to put maximum amount of data in the buffer.

Closing a Connection

Utilizing the close() function on the socket is normally advised when terminating a connection. Using close() is usually sufficient, and closing does not require an explicit call to the shutdown() function.

However, in some situations-particularly when dealing with an HTTP-like exchange-using the shutdown() method might be useful. It can continue to receive data if the client uses shutdown(1), for instance, which signifies that it has completed delivering data. The "EOF" (end of file) situation can be identified by the server when it receives 0 bytes, signifying that the client has finished making their request. Then, if required, the server can deliver a response. The client's continued ability to receive is verified if the transmit operation succeeds.

If the socket has yet to be explicitly closed, Python automatically closes it when it's garbage collected. The use of automated shutdown and closure is frowned upon, nonetheless. If a socket closes prematurely, it could result in problems like the other socket sending requests too slowly. Therefore, shutting the socket when the intended activities are finished explicitly is crucial.

You may guarantee that all essential cleanup and termination processes are carried out, avoiding any potential connection problems, by explicitly terminating the socket using close().

Tornado Framework

Tornado is a powerful Python web framework and asynchronous networking module. It is made to deal with high-performance demands, including managing hundreds of open connections at once. Its main feature is Tornado's non-blocking I/O architecture, which enables efficient management of numerous connections without the requirement for conventional thread-based concurrency.

The event-driven approach underlies Tornado's non-blocking I/O, where requests and answers are handled asynchronously. This means that Tornado may effectively manage numerous connections within a single thread rather than allocating a separate thread or process to handle each connection. This method drastically reduces context switching and thread synchronization costs, improving performance and scalability.

Tornado Framework over Normal Sockets

Tornado offers several benefits over traditional socket programming, making it a preferred choice for many developers:

  • Asynchronous I/O: Tornado is designed to manage numerous connections inside a single thread thanks to its asynchronous I/O architecture. As a result, there is no longer a requirement for thread-based parallelism, and the overhead of context switching and thread synchronization is decreased. As a result, Tornado can manage several connections at once while using fewer resources and performing better.
  • Scalability:Tornado is very scalable thanks to its non-blocking I/O design. Without many threads or processes, it can simultaneously handle thousands of open connections. Real-time apps and WebSockets are two examples of programs that demand great performance and must manage several concurrent clients.
  • Support for WebSockets:Tornado incorporates WebSockets, a well-liked technology for real-time communication between clients and servers. Tornado is perfectly suited for maintaining persistent WebSocket connections because of its asynchronous nature, enabling efficient and speedy real-time applications.
  • High-performance:Tornado is a high-performance framework thanks to its design and architecture. The Tornado can enable quicker response times and higher transactional performance by reducing the overhead associated with standard socket programming.
  • Comprehensive Features:Routing, template rendering, form handling, and authentication are just a few of the extensive capabilities and tools Tornado provides for creating online applications. While keeping its focus on asynchronous and high-performance networking, it offers a comprehensive framework for developing reliable and feature-rich web applications.
  • Community and ecology:The ecosystem of libraries and extensions for Tornado is expanding, and its development community is active. To increase their Tornado applications' functionality, developers may use existing libraries, access resources and tutorials, and receive help from the community.

As a whole, Tornado is a popular option for creating high-performance web applications and real-time communication systems due to its mix of an asynchronous I/O style, scalability, and WebSocket compatibility. It allows developers to create effective and quick applications while addressing large-scale networking requirements.

Let's understand the following example of simple Tornado WebSocket.

Example -

Output:

Socket Programming Using Python

Explanation:

In the above code,

  • We have defined the class ApplicationHandler which uses as the handler for request and return a response using the write()
  • The main() method is the entrance of the program.
  • The class web.Application creates the base for the web application and accepts a collection of handlers.
  • The Application listens on port 5000, and a client can communicate to this application using the same port.
  • The ioloop.IOLoop.instance().start() function is used to create a nonblocking thread for an application.

In this article, we have discussed the basics concepts of the network programming using the Python. We have defined the basic network terminologies and create the simple server and client. The Networking programming is vast field and hard to cover in the single tutorial but we are tried to cover all important concepts regarding network programming using Python.





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA