python: loop with socket.recv() -



python: loop with socket.recv() -

do know why loop doesn't break?

#!/usr/bin/env python socket import * import os import sys if __name__ == '__main__': host = '127.0.0.1' port = 55554 print 'creating socket' socketproxy = socket(af_inet, sock_stream) print 'bind()' socketproxy.setsockopt(sol_socket, so_reuseaddr, 1) socketproxy.bind((host, port)) print 'waiting connection request' socketproxy.listen(1) conn, addr = socketproxy.accept() print 'connected ', addr request = '' while true: info = conn.recv(16); if not data: break request = request+data print request sys.stdout.flush()

i writing little server-proxy getting requests can arbitrary long must wait until have received request. anyway loop (when len(data) == 0) doesn't stop , keeps on waiting. how can stop it? thanks

one solution create socket non-blocking. receive in loop until no more info received. if no info has been received (i.e. request empty) connection has been closed, otherwise have request.

when have request, send on actual server, , same above when waiting reply. send reply on client.

python python-sockets

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -