In this Python Socket Programming we want to learn about Python Socket Programming Basics, so Python is powerful programming language that is widely used in different sections such as web development, data science and automation. one of the most powerful features of Python is its ability to perform network programming using sockets. Sockets allows programs to communicate with each other over networks using TCP or IP protocols. in this article we want to to talk about basics of Python socket programming.
What is socket ?
Socket is an endpoint that establishes communication channel between two programs over network. it can be used to send and receive data over the network using TCP or IP protocols. socket is identified by unique IP address and a port number. IP address identifies host and port number identifies the specific service running on that host.
Python Socket Programming Basics
For creating a socket object, we use socket module in Python. socket module provides different methods to create socket object. we can create a socket object by calling socket() function and specifying the socket type and the protocol.
1 |
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
After creating socket object, we need to bind it to specific IP address and port number. this step is necessary to establish a communication channel between two programs. we can use bind() method of the socket object to bind it to a specific IP address and port number.
1 2 |
host = socket.gethostname() server_socket.bind((host, 9999)) |
After that the socket is bounded to specific IP address and port number, we can start listening for incoming connections. we can use listen() method of the socket object to listen for incoming connections. listen() method takes an argument that specifies the maximum number of connections that the socket can handle.
1 |
server_socket.listen(5) |
When a client requests a connection, server accepts the connection using accept() method of the socket object. accept() method returns a new socket object that represents the connection with the client. and server can use this new socket object to send and receive data with the client.
1 |
client_socket, address = server_socket.accept() |
After that the connection is established, client and server can send and receive data using send() and recv() methods of the socket object. send() method is used to send data from the client to the server, the recv() method is used to receive data from the server.
1 2 |
client_socket.send(b"Hello client") data = client_socket.recv(1024) |
This is is an example of simple server client program that uses sockets to communicate over the network.
This is our server side code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import socket # create socket object server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # get local machine name host = socket.gethostname() # bind socket to specific port server_socket.bind((host, 9999)) # listen for incoming connections server_socket.listen(5) # accept connections from clients client_socket, address = server_socket.accept() # send data to the client client_socket.send(b"Hello client") # receiv data from client data = client_socket.recv(1024) # print received data print("Received data:", data) # close the connection client_socket.close() |
This is our client side code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import socket # create socket object client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # get server hostname host = socket.gethostname() # connect the server client_socket.connect((host, 9999)) # receive data from the server data = client_socket.recv(1024) # print the received data print("Received data:", data) # send data to the server client_socket.send(b"Hello server") # clos the connection client_socket.close() |
You need to run the code in two terminal, one for server and the second for the client