admin管理员组文章数量:1345306
I want to establish a simple workflow between server and client to send and receive some - currently unknown amount of - text data:
- server is running, listening
- client starts, is set up for communication
- client sends text data to server
- server receives the message in chunks
- server uses it to perform some tasks
- server returns the results in chunks
- client receives all data
- 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()
本文标签: Python socket server recv() hangsStack Overflow
版权声明:本文标题:Python socket server recv() hangs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743782835a2538147.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论