tanszek:oktatas:iss_t:python_example_for_blocking_and_non-blocking_socket
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| tanszek:oktatas:iss_t:python_example_for_blocking_and_non-blocking_socket [2024/02/25 15:49] – létrehozva knehez | tanszek:oktatas:iss_t:python_example_for_blocking_and_non-blocking_socket [2024/02/25 15:55] (current) – knehez | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | === Blocking socket server === | ||
| + | |||
| + | To illustrate the difference with a blocking socket approach, we'll create a simple blocking TCP server and a corresponding client. This server will handle one connection at a time in a blocking manner, meaning it will wait (or block) on I/O operations like accepting new connections or receiving data. | ||
| + | |||
| + | <sxh python> | ||
| + | import socket | ||
| + | |||
| + | HOST = ' | ||
| + | PORT = 65432 # Port to listen on (non-privileged ports are > 1023) | ||
| + | |||
| + | # Create a socket | ||
| + | with socket.socket(socket.AF_INET, | ||
| + | server_socket.bind((HOST, | ||
| + | server_socket.listen() | ||
| + | print(f" | ||
| + | |||
| + | while True: | ||
| + | # Accept a new connection | ||
| + | conn, addr = server_socket.accept() | ||
| + | with conn: | ||
| + | print(f" | ||
| + | while True: | ||
| + | data = conn.recv(1024) | ||
| + | if not data: | ||
| + | break # No more data from client, close connection | ||
| + | print(f" | ||
| + | response = "This is a response from the server." | ||
| + | conn.sendall(response) | ||
| + | </ | ||
| + | |||
| + | === Blocking client === | ||
| + | <sxh python> | ||
| + | import socket | ||
| + | |||
| + | HOST = ' | ||
| + | PORT = 65432 # The port used by the server | ||
| + | |||
| + | # Create a socket | ||
| + | with socket.socket(socket.AF_INET, | ||
| + | s.connect((HOST, | ||
| + | print(" | ||
| + | |||
| + | # Send data | ||
| + | message = ' | ||
| + | s.sendall(message) | ||
| + | print(" | ||
| + | |||
| + | # Wait for a response | ||
| + | data = s.recv(1024) | ||
| + | print(" | ||
| + | |||
| + | print(f" | ||
| + | </ | ||
| + | |||
| + | ---- | ||
| === Non-blocking server === | === Non-blocking server === | ||
| + | |||
| + | Creating a non-blocking TCP socket server in Python involves setting up a socket to listen for connections without blocking the main execution thread of the program. Below is a simple example of a non-blocking TCP server that accepts multiple client connections and handles them asynchronously. This server uses the select method, which is a way to check for I/O readiness on sockets, making it possible to manage multiple connections without blocking on any single one. | ||
| <sxh python> | <sxh python> | ||
| Line 67: | Line 124: | ||
| outputs.remove(s) | outputs.remove(s) | ||
| s.close() | s.close() | ||
| + | </ | ||
| + | |||
| + | === Non-blocking client === | ||
| + | |||
| + | To test the non-blocking TCP server, you can create a simple client that connects to the server, sends a message, and then waits to receive a response. Below is an example of a basic TCP client in Python that interacts with our non-blocking server. | ||
| + | |||
| + | <sxh python> | ||
| + | import socket | ||
| + | |||
| + | HOST = ' | ||
| + | PORT = 65432 # The port used by the server | ||
| + | |||
| + | # Create a socket | ||
| + | with socket.socket(socket.AF_INET, | ||
| + | # Connect to the server | ||
| + | s.connect((HOST, | ||
| + | print(" | ||
| + | |||
| + | # Send data | ||
| + | message = ' | ||
| + | s.sendall(message) | ||
| + | print(" | ||
| + | |||
| + | # Wait for a response | ||
| + | data = s.recv(1024) | ||
| + | print(" | ||
| + | |||
| + | print(f" | ||
| </ | </ | ||
tanszek/oktatas/iss_t/python_example_for_blocking_and_non-blocking_socket.1708876160.txt.gz · Last modified: 2024/02/25 15:49 by knehez
