In this Python Socket article we want to talk about Python Socket Client Example, so Python is powerful and famous programming language that allows you to build different types of applications, including network applications that can communicate over the internet. in this article we want to talk about Python socket client example and how to use it to build network applications, but first of all let’s talk that what is socket ?
What is Socket ?
Socket is a software abstraction that represents an endpoint of two way communication link between two programs running on a network. in simpler terms we can say that socket allows two programs to send and receive data between them over a network.
Python Socket Client Example
Let’s start by building Python socket client that can connect to a server and send and receive data. this code shows simple Python socket client example, this is our client code and save the code in client.py file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import socket # Create a socket object client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Connect to a remote server server_address = ('localhost', 8000) client_socket.connect(server_address) # Send data to the server message = 'Hello server - Iam from geekscoders.com' client_socket.sendall(message.encode()) # Receive data from the server data = client_socket.recv(1024) print(f'Received data: {data.decode()}') # Close the socket connection client_socket.close() |
In the above code we have created a socket object using the socket.socket() method. first argument socket.AF_INET specifies the address family (IPv4), and the second argument socket.SOCK_STREAM specifies the socket type (TCP), and after that we have connected to remote server using the socket.connect() method. server_address variable specifies the IP address and port number of the server, than we send data to the server using the socket.sendall() method. message is encoded as bytes using the encode() method, after that we receives data from the server using socket.recv() method. 1024 argument specifies the maximum amount of data to be received at once, and lastly we print the received data using print() function and close the socket connection.
Now let’s create our server code, save the file in server.py file
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 29 30 31 32 33 |
import socket # Define host and port HOST = 'localhost' PORT = 8000 # Create socket object server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind socket to specific address and port server_socket.bind((HOST, PORT)) # Listen for incoming connections server_socket.listen(1) # Wait for client to connect print(f"Server listening on {HOST}:{PORT}...") client_socket, address = server_socket.accept() print(f"Connection from {address} has been established") # Send welcome message to the client message = "Welcome to the server" client_socket.send(message.encode()) # Receive data from the client data = client_socket.recv(1024) print(f"Received data: {data.decode()}") # Close the client connection client_socket.close() # Close the server socket server_socket.close() |
This code creates socket object and binds it to specific address and port, listens for incoming connections and waits for a client to connect. after that the client is connected, it sends welcome message and receives data from the client before closing the connection. Note that this is very basic example and in practice, you would want to handle multiple client connections, handle errors and exceptions and implement more advanced functionality as needed.
Run your code in two terminal this is our server terminal.
And this is our client terminal
So in this article we learned how to build Python socket example and use it to communicate with remote server over a network. with this knowledge you can now start building your own network applications using Python.