[Python] Python socket server recv() hangs

Discussão em 'Python' iniciado por Stack, Abril 13, 2025.

  1. Stack

    Stack Membro Participativo

    I want to establish a simple workflow between server and client to send and receive some - currently unknown amount of - text data:

    1. server is running, listening
    2. client starts, is set up for communication
    3. client sends text data to server
    4. server receives the message in chunks
    5. server uses it to perform some tasks
    6. server returns the results in chunks
    7. client receives all data
    8. connection is closed, server goes on listening

    I did some research read up on blocking and non-blocking sockets - I'm totally fine with the blocking version - and came up with the following code. What works are points 0 - 2: setup, starting and sending the message.

    At 3. server receives the data, but then waits indefinitely instead of recognizing that no more data is coming - if not chunk: never actually executes although the full data is transferred so the while loop is never broken. I tried to change the condition to e.g. match the double curly braces at the end of the last chunk, but no success.

    Now, my understanding is that server should realize that no more chunks of data is coming, at least this is what all the examples suggested. Note, the real data is longer than the example so transferring in chunks is essential.

    What am I doing wrong?

    import threading
    import socket
    import json
    import time


    class RWF_server:

    def start_server(self):

    host_address = 'localhost'
    port = 9999
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
    server.bind((host_address, port))
    except OSError:
    raise
    print(f"Could not bind to {host_address}:{port}, is a server already running here?")
    server.listen(5)
    print(f"Server listening on {host_address}:{port}")

    while True:
    conn, addr = server.accept()
    print(f'Connected by {addr}')
    data = ""
    while True:
    chunk = conn.recv(1024).decode('utf-8')
    print('Server got:', chunk)
    if not chunk:
    break
    data += chunk
    if not data:
    continue # Continue to the next connection if no data was received
    message = json.loads(data)
    print(f'Received full message: {message}, getting to work')

    # Simulate processing
    time.sleep(3)
    print('Task is done')

    response = {'status': 'success', 'message': 'Data received'}
    response_data = json.dumps(response)
    conn.sendall(response_data.encode('utf-8'))
    conn.close()


    class RWF_client:

    def start_client(self):

    content = {2: 'path_to_a_file\\__batch_B3310.bat',
    'data': {'FY_phase': 0.0,
    'MY_phase': 0.0,
    'head_chamfer_legth': 0.1,
    'head_radius': 2900.0,
    'head_thickness': 13.0,
    'head_thickness_at_nozzle': 13.0, }}

    print('The following content is sent to the server: {}'.format(content))

    with socket.socket() as sock:
    try:
    sock.connect(('localhost', 9999))
    except (ConnectionRefusedError, TimeoutError):
    raise

    sock.sendall(json.dumps(content).encode('utf-8'))
    print('Content sent to the server')

    response = ""
    while True:
    chunk = sock.recv(1024).decode('utf-8')
    print('Client getting response: {}'.format(chunk))
    if not chunk:
    break
    response += chunk

    print(f'Received response: {json.loads(response)}')

    response = json.loads(response)

    return response


    server_thread = threading.Thread(target=RWF_server().start_server)
    server_thread.start()

    client_thread = threading.Thread(target=RWF_client().start_client)
    client_thread.start()

    server_thread.join()
    client_thread.join()

    Continue reading...

Compartilhe esta Página