Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. Networking
  5. Client and Server

Client and Server

In python, we have a module named as socket which helps in networking between client and the server.

We can easily import socket module in python. We don’t need to install it from anywhere. If you are downloading it through pip install it means you are installing os from pip install.


import socket

socket module has a function known as socket(). It is used to create a socket.


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

socket_family- This is either AF_UNIX or AF_INET.
Socket_type- This is either SOCK_STREAM or SOCK_DGRAM.
protocol- This is usually left out, defaulting to 0.


  • bind()– This method binds address (hostname, port number pair) to socket.
  • listen()– This method sets up and start TCP listener.
  • accept()– This passively accept TCP client connection, waiting until connection arrives.
  • connect()– This method actively initiates TCP server connection.
  • send()– This method transmits TCP message.
  • recv()– This method receives TCP message.
  • close()– This method closes the socket.


To write Internet servers, we use the socket function available in socket module to create a socket object.

bind(), listen() and accept() are server socket methods.


A simple client program opens a connection to a given port 12345 and given host. This is very simple to create a socket client using Python’s socket module function.

connect() is the client socket method.

This is all about client and server in python.

Was this article helpful to you? Yes No

How can we help?