How to create socket in Python programming

How to create socket in Python programming

Satya Python solution , , ,

How to create socket in Python programming: Socket is network interface, where two nodes in a network connect and communicate. In this, server program in a server listen on particular port and accept incoming connection from another socket program(client) from another nodes in a network.

In this post, I will explain how to create socket in Python programming.


Server side program to create listening socket.

In the server side programing first thing we have to do is, load the socket module. After that, with the help of socket function create TCP/IP socket.

Socket function uses two argument. These are AF_INET and SOCK_STREAM. AF_INET define Internet Protocol v4 addresses. This is the most used option  to create socket. You can also use AF_INET6, which define Internet Protocol v6 addresses.

SOCK_STREAM is used for defining TCP protocol. If you want you can use SOCK_DGRAM, that is for UDP.  But TCP is best network programming.

After creating socket instance, you have to bind the host with port.This can be done by using bind function. It accept host IP address and port number as an arguments. There after, we will define listen function, this will make sure the program will listen at this port.

In a while loop, use socket instance to call accept function. Here it will  accept the incoming connection from client program in another node. Here I have written inner loop to call the recv function. This helps in receiving the incoming data stream from client socket program.

Server socket program

#!/usr/bin/env python3 
import socket
HOST = socket.gethostname() 
PORT = 8899 # Port to listen on (non-privileged ports are > 1023) 
sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)

sock.bind((HOST, PORT)) 

while True:
   print ('Waiting for a connection....')
   conn, client_addr = sock.accept()

       print ('connection from', client_addr)

       while True:
           data = conn.recv(64)
           if data:
               print ("Data Recieved: %s" % data)
               print ("no more data.")

Client socket program to send data to listening socket.

In a client program, create socket using socket.socket() function and then get the ip address of the server host(listening socket host). Using socket instance, call connect() function to connect  server host at the listening port. In this case it is 8899. After this, use sendall function to send data to listening port of a server in a network. This data will be received by recv() function of a server program.


Client socket program

import socket
import time

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# assign hostname of the server program(listening socket), replace server_hostname with your server hostname
hostname = "server_hostname"

ip_address = socket.gethostbyname(hostname)

server_address = (ip_address, 8899)


numbers_data = ["88", "43", "34", "12", "33", "91","74","66"]

for num in numbers_data:
    print ("Data sent: %s" % num)
    new_dat = str("%s\n" % num).encode("utf-8")




For more detail on this refer network socket.


You May Also Like..

How to concatenate string in Python

How to concatenate string in Python

How to concatenate string in Python: Concatenating a string is very important feature in any computer programming language. Python provide […]

Multi threading program in python

Multi threading program in python

Multi threading program in python: Multi-Thread is a multiple processes running concurrently in one CPU core processor. It is different […]

Read and write CSV file using python

Read and write CSV file using python : CSV file, which is comma separated file, is the most popular form […]

Leave a Reply

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