In this Python article we want to talk about Python Socket Programming Examples, so Python is powerful programming language that can be used for different types of applications, including socket programming. Socket programming is the process of creating network connections between two computers and it allows them to communicate with each other. in this article we want to provide examples of socket programming in Python, covering both TCP and UDP protocols, as well as multithreading.
TCP Socket Programming in Python
TCP (Transmission Control Protocol) is reliable and connection oriented protocol used for transmitting data over the internet. this Python code creates simple TCP server that listens for connections on port 8000.
1 2 3 4 5 6 7 8 9 10 11 12 |
import socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind(('localhost', 8000)) server_socket.listen(1) while True: client_socket, address = server_socket.accept() print(f"Connection from {address} has been established!") response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body><h1>Welcome to geekscoders.com server</h1></body></html>" client_socket.send(response.encode()) client_socket.close() |
In the above code socket module is used to create socket object, after that it is bounded to specific IP address and port number. listen() method is called to start listening for incoming connections, and the server enters an infinite loop to continuously accept new connections. when a connection is accepted, accept() method returns new socket object that can be used to communicate with the client. in this example we simply send welcome message to the client and then close the connection.
If you go to http://localhost:8000/, this will be the result
UDP Socket Programming in Python
UDP (User Datagram Protocol) is a connectionless and unreliable protocol used for transmitting data over the internet. this Python code creates simple UDP client that sends a message to a server.
1 2 3 4 5 |
import socket client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) message = "Hello, server!" client_socket.sendto(message.encode(), ('localhost', 8000)) |
in the above code socket module is used to create socket object, after that it is used to send a message to the server using sendto() method. Note that unlike TCP there is no connection setup or teardown in UDP – messages are simply sent and received without any reliability guarantees.
Multithreaded Socket Programming in Python
Multithreading allows a program to perform multiple tasks simultaneously, which is useful for socket programming when multiple clients need to be served simultaneously. this Python code creates a multithreaded TCP server that can handle multiple clients.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import socket import threading def handle_client(client_socket, address): print(f"Connection from {address} has been established!") client_socket.send("Welcome to the server!".encode()) client_socket.close() server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind(('localhost', 8000)) server_socket.listen(5) while True: client_socket, address = server_socket.accept() t = threading.Thread(target=handle_client, args=(client_socket, address)) t.start() |
In this example a new thread is created to handle each client connection. handle_client() function is called in each thread to serve the client, sending welcome message and after that closing the connection.
Learn More
- How to Download YouTube Videos in Python
- Working with Python Pyglet Library
- Python Speech Recognition For Beginners
- PyQtGraph Tutorial in Python
- Convert Python PY File to EXE