Хостом является мой ноутбук, и на нем работает этот код:
< pre class="lang-py Prettyprint-override">
Код: Выделить всё
import socket
import requests
# Function to handle the request
def function1(code, client_address):
grade = code[:2]
version = code[2].lower()
session = code[3]
if version not in ["p", "o"]:
response = f"({version}) Version not found."
elif grade not in ["10", "11"]:
response = f"Grade ({grade}) not found."
elif session not in ["1", "2", "3", "4", "5"]:
response = f"Session ({session}) not found."
else:
response = "Request processed successfully."
# Send the response back to the client
requests.post(f"http://{client_address[0]}:{client_address[1]}",response)
# Set up server socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('0.0.0.0', 3333)) # Listen on all interfaces on port 3333
server_socket.listen(1) # Listen for one incoming connection
print("Server listening for incoming connections...")
while True:
client_socket, client_address = server_socket.accept() # Accept incoming connection
print(f"Connection established with {client_address}")
# Receive data from the client
data = client_socket.recv(1024).decode()
print(f"Received data: {data}")
# Call the function to handle the request and send back the response
function1(data, client_address)
client_socket.close() # Close the connection
Код: Выделить всё
echo "10P7" | nc 192.168.150.73 3333
Код: Выделить всё
Traceback (most recent call last):
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='192.168.150.182', port=34976): Max retries exceeded with url: / (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
requests.exceptions.ConnectionError: HTTPConnectionPool(host='192.168.150.182', port=34976): Max retries exceeded with url: / (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
Код: Выделить всё
cat
Подробнее здесь: [url]https://stackoverflow.com/questions/78237900/not-getting-response-from-host-python[/url]