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)) sock.listen(1) while True: print ('Waiting for a connection....') conn, client_addr = sock.accept() try: print ('connection from', client_addr) while True: data = conn.recv(64) if data: print ("Data Recieved: %s" % data) else: print ("no more data.") break finally: conn.close()
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) sock.connect(server_address) 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") sock.sendall(new_dat) time.sleep(3) sock.close()
For more detail on this refer network socket.