def netstring_server(server_socket): "A basic netstring server loop, supporting a few operations" running = True try: while running: clientsock, addr = server_socket.accept() client = NetstringSocket(clientsock) while 1: request = client.read_ns() if request == b'close': clientsock.close() break elif request == b'shutdown': running = False break elif request == b'reply4k': client.write_ns(b'a' * 4096) elif request == b'ping': client.write_ns(b'pong') elif request == b'reply128k': client.setmaxsize(128 * 1024) client.write_ns(b'huge' * 32 * 1024) # 128kb client.setmaxsize(32768) # back to default except Exception as e: print(u'netstring_server exiting with error: %r' % e) raise return
def netstring_server(server_socket): "A basic netstring server loop, supporting a few operations" running = True while running: clientsock, addr = server_socket.accept() client = NetstringSocket(clientsock) while 1: request = client.read_ns() if request == b'close': clientsock.close() break elif request == b'shutdown': running = False break elif request == b'reply4k': client.write_ns(b'a' * 4096) elif request == b'ping': client.write_ns(b'pong') return
def netstring_server_timeout_override(server_socket): """Netstring socket has an unreasonably low timeout, however it should be overriden by the `read_ns` argument.""" try: while True: clientsock, addr = server_socket.accept() client = NetstringSocket(clientsock, timeout=0.01) while 1: request = client.read_ns(1) if request == b'close': clientsock.close() break elif request == b'shutdown': return elif request == b'ping': client.write_ns(b'pong') except Exception as e: print(u'netstring_server exiting with error: %r' % e) raise