Python - Network Programming

In this chapter of about Python - Network Programming.

Python have two levels of access to network services.

Python also has libraries that provide higher-level access to specific application-level network protocols, such as FTP, HTTP, and so on.

Socket Introduction

Sockets is communicate within a process, between processes on the same machine, or between processes on different continents.

A socket is a link between two applications that can communicate with one another (either locally on a single machine or remotely between two machines in separate locations).

Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP, and so on

Sr.No. Term & Description


The family of protocols that is used as the transport mechanism. These values are constants such as AF_INET, PF_INET, PF_UNIX, PF_X25, and so on.



The type of communications between the two endpoints, mostly SOCK_STREAM for connection-oriented protocols and SOCK_DGRAM for connectionless protocols.



It is used to identify a variant of a protocol within a domain and type.



The identifier of a network interface −

  • A string, which can be a host name, a dotted-quad address, or an IPV6 address in colon (and possibly dot) notation

  • A string "<broadcast>", which specifies an INADDR_BROADCAST address.

  • A zero-length string, which specifies INADDR_ANY, or

  • An Integer, interpreted as a binary address in host byte order.



Each server listens for clients calling on one or more ports.

A port may be a Fixnum port number, a string containing a port number, or the name of a service.

The socket Module

In order to create a socket, you use the socket.socket() function, and the syntax is as simple as:

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

Here is the description of the arguments:

  • socket_family: Represents the address (and protocol) family. It can be either AF_UNIX or AF_INET.
  • socket_type: Represents the socket type, and can be either SOCK_STREAM or SOCK_DGRAM.
  • protocol: This is an optional argument, and it usually defaults to 0.

Client Socket Methods

Sr.No. Method & Description


This method actively initiates TCP server connection.

Build a Simple Server

Let's we can look at a simple Python server. The following are the socket server methods available in Python. A socket object is then used to call other functions to setup a socket server. There are following
  • s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  • s.bind(): Binds address (hostname, port number) to socket.
  • s.listen(): Sets up and starts TCP listener.
  • s.accept(): Accepts TCP client connection.
import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Define host
host = 'localhost'

# define the communication port
port = 8080

# Bind the socket to the port
sock.bind((host, port))
# Listen for incoming connections

# Wait for a connection
print 'waiting for a connection'
connection, client = sock.accept()

print client, 'connected'

# Receive the data in small chunks and retransmit it

data = connection.recv(16)
print 'received "%s"' % data
if data:

    print 'no data from', client

# Close the connection

Server SIDE Output

$ python server.py
waiting for a connection
('', 47050) connected
received "message"

Client SIDE Output

$ python client.py
socket closed

Welookups is optimized for learning.© 2018 - KUMAR ADITYA SINGH .
All Right Reserved and you agree to have read and accepted our term and condition.
All Rights Reserved.