Socket Programming using Python #2


Socket Programming

PYTHON Tutorial #2


Hi guys!!! Today were going to learn how to create and destroy sockets on your system and how to connect to system’s.


To communicate on the network we create a new socket object using the socket function present in the socket module.

SYNTAX : socket(family,type[,proto]) .

 The family is usually AF_INET ,but you can also use AF_IPX  ,depending on the platform. The type  most often used is SOCK_STREAM(for TCP)and SOCK_DGRAM(for UDP).

To close the  connection the socket is closed using the close function.



when two sockets connect, one side listen’s for and accepts an incoming request and the other side initiates that connection. The listening side creates a socket and binds it to a particular address and port. then finally we initiate the listening, when ever a request is encountered the request is accepted by the accept function.


After you executed the accept function the interpreter will wait for a request until it finds one. If you see the bind function properly we have used the loopback IP address , so now  your computer is listening to your computer .Now to end the wait we will create a request on your computer. Open another python interpreter and do the following :

     STEP 1) Open a another python interpreter .

     STEP 2) Write the following commands.


Once you done this , if you see the previous interpreter it would be like this;


This means that a connection has been made between both the programs (in this case the Python interpreter ).

Once you have established the connection now you can transmit data  through it.

Listening side                                                      

Request side


Now let us discuss what the above given functions do:

  •  Bind(address): This function binds the socket and the address together. The address consist of an IP address and a port number.
  •  Connect(address): This connects your computer to the given address, only if a socket is present on the address and it is listening on that particular socket.
  •  Listen(backlog): It listen’s to incoming request’s on the given address. The backlog argument provides a hint to the implementation, which the implementation shall use to limit the number of outstanding connections in the socket’s listen queue. For the time being we shall give it as 1.
  •  Accept(): It initiates the listening and waits for an incoming request. It returns a new object of socket type and its address.
  •  Recv(size): It initiates the receiving of data from the source .size defines the maximum number of bytes it can receive in a single go.
  •  Send(message): It initiates the transmission of data over the network (to the connected socket). Message can be anything from a string to a variable .

 *NOTE: UDP sockets are not connection oriented , but you can still use the connect command to associate the destination address and port. 

With this we finish the python module, next time we would be learning how to send and receive data in a more complex and precise manner.

Leave a Reply

Your email address will not be published. Required fields are marked *