Python - Socket Programming



The socket module in the standard library included functionality required for communication between server and client at hardware level.

This module provides access to the BSD socket interface. It is available on all operating systems such as Linux, Windows, MacOS.

What are Sockets?

Sockets are the endpoints of a bidirectional communications channel. Sockets may communicate within a process, between processes on the same machine, or between processes on different continents.

A socket is identified by the combination of IP address and the port number. It should be properly configured at both ends to begin communication.

connection IP_address

Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP, and so on. The socket library provides specific classes for handling the common transports as well as a generic interface for handling the rest.

The term socket programming implies programmatically setting up sockets to be able to send and receive data.

There are two types of communication protocols −

  • connection-oriented protocol

  • connection-less protocol

TCP or Transmission Control Protocol is a connection-oriented protocol. The data is transmitted in packets by the server, and assembled in the same order of transmission by the receiver. Since the sockets at either end of the communication need to be set before starting, this method is more reliable.

UDP or User Datagram Protocol is connectionless. The method is not reliable because the sockets does not require establishing any connection and termination process for transferring the data.

The Python socket Module

This module includes Socket class. A socket object represents the psir of hostname and port number. The constructor method has the following signature −

Syntax

socket.socket (socket_family, socket_type, protocol=0)

Parameters

  • family − AF_INET by default. Other values - AF_INET6 (eight groups of four hexadecimal digits), AF_UNIX, AF_CAN (Controller Area Network) or AF_RDS (Reliable Datagram Sockets).

  • socket_type − should be SOCK_STREAM (the default), SOCK_DGRAM, SOCK_RAW or perhaps one of the other SOCK_ constants.

  • protocol − number is usually zero and may be omitted.

Return Type

This method returns a socket object.

Once you have the socket object, then you can use the required methods to create your client or server program.

Server Socket Methods

The socket instantiated on server is called server socket. Following methods are available to the socket object on the server −

  • bind() method − This method binds the socket to specified IP address and port number.

  • listen() method − This method starts server and runs into a listen loop looking for connection request from client.

  • accept() method − When connection request is intercepted by server, this method accepts it and identifies the client socket with its address.

To create a socket on a server, use the following snippet −

import socket
server = socket.socket()
server.bind(('localhost',12345))
server.listen()
client, addr = server.accept()
print ("connection request from: " + str(addr))

By default, the server is bound to local machine's IP address 'localhost' listening at arbitrary empty port number.

Client Socket Methods

Similar socket is set up on the client end. It mainly sends connection request to server socket listening at its IP address and port number

connect() method

This method takes a two-item tuple object as argument. The two items are IP address and port number of the server.

obj=socket.socket()
obj.connect((host,port))

Once the connection is accepted by the server, both the socket objects can send and/or receive data.

send() method

The server sends data to client by using the address it has intercepted.

client.send(bytes)

Client socket sends data to socket it has established connection with.

sendall() method

similar to send(). However, unlike send(),this method continues to send data from bytes until either all data has been sent or an error occurs. None is returned on success.

sendto() method

This method is to be used in case of UDP protocol only.

recv() method

This method is used to retrieve data sent to the client. In case of server, it uses the remote socket whose request has been accepted.

client.recv(bytes)

recvfrom() method

This method is used in case of UDP protocol.

Python - Socket Server

To write Internet servers, we use the socket function available in socket module to create a socket object. A socket object is then used to call other functions to setup a socket server.

Now call the bind(hostname, port) function to specify a port for your service on the given host.

Next, call the accept method of the returned object. This method waits until a client connects to the port you specified, and then returns a connection object that represents the connection to that client.

import socket
host = "127.0.0.1"
port = 5001
server = socket.socket()
server.bind((host,port))
server.listen()
conn, addr = server.accept()
print ("Connection from: " + str(addr))
while True:
   data = conn.recv(1024).decode()
   if not data:
      break
   data = str(data).upper()
   print (" from client: " + str(data))
   data = input("type message: ")
   conn.send(data.encode())
conn.close()

Python - Socket Client

Let us write a very simple client program, which opens a connection to a given port 5001 and a given localhost. It is very simple to create a socket client using the Python's socket module function.

The socket.connect(hosname, port) opens a TCP connection to hostname on the port. Once you have a socket open, you can read from it like any IO object. When done, remember to close it, as you would close a file.

The following code is a very simple client that connects to a given host and port, reads any available data from the socket, and then exits when 'q' is entered.

import socket
host = '127.0.0.1'
port = 5001
obj = socket.socket()
obj.connect((host,port))
message = input("type message: ")
while message != 'q':
   obj.send(message.encode())
   data = obj.recv(1024).decode()
   print ('Received from server: ' + data)
   message = input("type message: ")
obj.close()
  • Run Server code first. It starts listening.

  • Then start client code. It sends request.

  • Request accepted. Client address identified

  • Type some text and press Enter.

  • Data received is printed. Send data to client.

  • Data from server is received.

  • Loop terminates when 'q' is input.

Server-client interaction is shown below −

server_client_interaction

We have implemented client-server communication with socket module on the local machine. To put server and client codes on two different machines on a network, we need to find the IP address of the server machine.

On Windows, you can find the IP address by running ipconfig command. The ifconfig command is the equivalent command on Ubuntu.

ipv4_address

Change host string in both the server and client codes with IPv4 Address value and run them as before.

Python File Transfer with Socket Module

The following program demonstrates how socket communication can be used to transfer a file from server to the client

Server Code

The code for establishing connection is same as before. After the connection request is accepted, a file on server is opened in binary mode for reading, and bytes are successively read and sent to the client stream till end of file is reached.

import socket
host = "127.0.0.1"
port = 5001
server = socket.socket()
server.bind((host, port))
server.listen()
conn, addr = server.accept()
data = conn.recv(1024).decode()
filename='test.txt'
f = open(filename,'rb')
while True:
   l = f.read(1024)
   if not l:
      break
   conn.send(l)
   print('Sent ',repr(l))
f.close()
print('File transferred')
conn.close()

Client Code

On the client side, a new file is opened in wb mode. The stream of data received from server is written to the file. As the stream ends, the output file is closed. A new file will be created on the client machine.

import socket

s = socket.socket()
host = "127.0.0.1"
port = 5001

s.connect((host, port))
s.send("Hello server!".encode())

with open('recv.txt', 'wb') as f:
   while True:
      print('receiving data...')
      data = s.recv(1024)
      if not data:
         break
      f.write(data)
      
f.close()
print('Successfully received')
s.close()
print('connection closed')

The Python socketserver Module

The socketserver module in Python's standard library is a framework for simplifying task of writing network servers. There are following classes in module, which represent synchronous servers −

socketserver_module

These classes work with corresponding RequestHandler classes for implementing the service. BaseServer is the superclass of all Server objects in the module.

TCPServer class uses the internet TCP protocol, to provide continuous streams of data between the client and server. The constructor automatically attempts to invoke server_bind() and server_activate(). The other parameters are passed to the BaseServer base class.

You must also create a subclass of StreamRequestHandler class. IT provides self.rfile and self.wfile attributes to read or write to get the request data or return data to the client.

  • UDPServer and DatagramRequestHandler − These classes are meant to be used for UDP protocol.

  • DatagramRequestHandler and UnixDatagramServer − These classes use Unix domain sockets; they're not available on non-Unix platforms.

Server Code

You must write a RequestHandler class. It is instantiated once per connection to the server, and must override the handle() method to implement communication to the client.

import socketserver
class MyTCPHandler(socketserver.BaseRequestHandler):
   def handle(self):
      self.data = self.request.recv(1024).strip()
      host,port=self.client_address
      print("{}:{} wrote:".format(host,port))
      print(self.data.decode())
      msg=input("enter text .. ")
      self.request.sendall(msg.encode())

On the server's assigned port number, an object of TCPServer class calls the forever() method to put the server in the listening mode and accepts incoming requests from clients.

if __name__ == "__main__":
   HOST, PORT = "localhost", 9999
   with socketserver.TCPServer((HOST, PORT), MyTCPHandler) as server:
      server.serve_forever()

Client Code

When working with socketserver, the client code is more or less similar with the socket client application.

import socket
import sys

HOST, PORT = "localhost", 9999

while True:
   with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
      # Connect to server and send data
      sock.connect((HOST, PORT))
      data = input("enter text .. .")
      sock.sendall(bytes(data + "\n", "utf-8"))
      
      # Receive data from the server and shut down
      received = str(sock.recv(1024), "utf-8")
      print("Sent: {}".format(data))
      print("Received: {}".format(received))

Run the server code in one command prompt terminal. Open multiple terminals for client instances. You can simulate a concurrent communication between the server and more than one clients.

Server Client-1 Client-2

D:\socketsrvr>python myserver.py

127.0.0.1:54518 wrote:

from client-1

enter text ..

hello

127.0.0.1:54522 wrote:

how are you

enter text ..

fine

127.0.0.1:54523 wrote:

from client-2

enter text ..

hi client-2

127.0.0.1:54526 wrote:

good bye

enter text ..

bye bye

127.0.0.1:54530 wrote:

thanks

enter text ..

bye client-2

D:\socketsrvr>python myclient.py

enter text .. .

from client-1

Sent:

from client-1

Received: hello

enter text .. .

how are you

Sent:

how are you

Received: fine

enter text .. .

good bye

Sent: good bye

Received: bye bye

enter text .. .

D:\socketsrvr>python myclient.py

enter text .. .

from client-2

Sent:

from client-2

Received: hi client-2

enter text .. .

thanks

Sent: thanks

Received:

bye client-2

enter text .. .

Advertisements