From a33d49ac4d1ce554c72770927957a089b4023bf1 Mon Sep 17 00:00:00 2001 From: pants Date: Mon, 21 Sep 2026 14:17:45 -0700 Subject: init --- python/sockets/client.py | 21 +++++++++++++++++++++ python/sockets/server.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 python/sockets/client.py create mode 100644 python/sockets/server.py (limited to 'python/sockets') diff --git a/python/sockets/client.py b/python/sockets/client.py new file mode 100644 index 0000000..c607065 --- /dev/null +++ b/python/sockets/client.py @@ -0,0 +1,21 @@ +import socket + +# Create a socket object +client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + +# Define the host and port to connect to +host = '10.100.54.10' +port = 8000 + +# Connect to the server +client_socket.connect((host, port)) + +# Send data to the server +client_socket.sendall("Hello from the client!".encode()) + +# Receive a response from the server +response = client_socket.recv(1024) +print("Response from server:", response.decode()) + +# Close the connection +client_socket.close() diff --git a/python/sockets/server.py b/python/sockets/server.py new file mode 100644 index 0000000..aba4a9a --- /dev/null +++ b/python/sockets/server.py @@ -0,0 +1,39 @@ +import socket + +# Create a socket object +server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + +# Define the host and port on which the server will listen +host = '127.0.0.1' +port = 8000 + +# Bind the socket to the host and port +server_socket.bind((host, port)) + +# Listen for incoming connections +server_socket.listen() + +print("Server listening on {}:{}".format(host, port)) + +while True: + # Accept a connection + client_socket, client_address = server_socket.accept() + print("Connection established with", client_address) + + while True: + # Receive data from the client + data = client_socket.recv(1024) + if not data: + # If no data is received, client has closed the connection + print("Connection closed by", client_address) + break + print("Received:", data.decode()) + + # Send a response back to the client + client_socket.sendall("Hello from the server!".encode()) + + # Close the client socket + client_socket.close() + +# Close the server socket (this part will never be reached in this loop) +server_socket.close() -- cgit v1.2.3