Sockets

In this section, we are going to learn about sockets. We are going to use Python's socket module. Sockets are endpoints for communication between machines, whether locally or across the internet. The socket module has a socket class, which is used to handle the data channel. It also has functions for network-related tasks. To use the functionality of the socket module, we first need to import the socket module.

Let's see how to create a socket. The socket class has a socket function, with two arguments: address_family and socket type.

The following is the syntax:

            import socket
s = socket.socket(address_family, socket type)

address_family controls the OSI network layer protocol.

socket type controls the transport layer protocol.

Python supports three address families: AF_INET, AF_INET6, and AF_UNIX. The most commonly used is AF_INET, which is used for internet addressing. AF_INET6 is used for IPv6 internet addressing. AF_UNIX is used for Unix Domain Sockets (UDS), which is an inter-process communication protocol.

There are two socket types: SOCK_DGRAM and SOCK_STREAM. The SOCK_DGRAM socket type is used for message-oriented datagram transport; these are associated with the UDP. Datagram sockets deliver individual messages. SOCK_STREAM is used for stream-oriented transport; these are associated with TCP. Stream sockets provide byte streams between the client and server.

Sockets can be configured as server and client sockets. When both TCP/IP sockets are connected, communication will be bi-directional. Now we are going explore an example of client-server communication. We will create two scripts: server.py and client.py.

The server.py script is as follows:

import socket

host_name = socket.gethostname()
port = 5000
s_socket = socket.socket()
s_socket.bind((host_name, port))
s_socket.listen(2)

conn, address = s_socket.accept()
print("Connection from: " + str(address))

while True:
recv_data = conn.recv(1024).decode()
if not recv_data:
break
print("from connected user: " + str(recv_data))
recv_data = input(' -> ')
conn.send(recv_data.encode())

conn.close()

Now we will write a script for the client.

The client.py script is as follows:

import socket

host_name = socket.gethostname()
port = 5000

c_socket = socket.socket()
c_socket.connect((host_name, port))
msg = input(" -> ")

while msg.lower().strip() != 'bye':
c_socket.send(msg.encode())
recv_data = c_socket.recv(1024).decode()
print('Received from server: ' + recv_data)
msg = input(" -> ")

c_socket.close()

Now we will run these two programs in two different Terminals. In the first Terminal, we'll run server.py, and in the second terminal, run client.py.

The output will be as follows:

Terminal 1: python3 server.py

Terminal 2: python3 client.py

student@ubuntu:~/work$ python3 server.py

Connection from: ('127.0.0.1', 35120)

from connected user: Hello from client

 -> Hello from server !

student@ubuntu:~/work$ python3 client.py

-> Hello from client

Received from server: Hello from server !

 ->

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset