Issue
I'm writing a fast python server that handles 128 bites transferred by TCP and executes serial number and command from it. I found async example of server and changes it for my needs, but after a successful connection, it doesn't see TCP packets, here is the server:
#aioserver.py
def accept_client(client_reader, client_writer):
task = asyncio.Task(handle_client(client_reader, client_writer))
clients[task] = (client_reader, client_writer)
def client_done(task):
del clients[task]
client_writer.close()
log.info("End Connection")
log.info("New Connection")
task.add_done_callback(client_done)
async def handle_client(client_reader, client_writer):
# give client a chance to respond, timeout after 10 seconds
try:
data = await asyncio.wait_for(client_reader.readline(), timeout=10.0)
print(data)
except asyncio.exceptions.TimeoutError:
data = None
if data is None:
log.warning("Expected WORLD, received None")
return
def main():
loop = asyncio.get_event_loop()
f = asyncio.start_server(accept_client, host=None, port=8080)
loop.run_until_complete(f)
loop.run_forever()
if __name__ == '__main__':
log = logging.getLogger("")
formatter = logging.Formatter("%(asctime)s %(levelname)s " +
"[%(module)s:%(lineno)d] %(message)s")
# setup console logging
log.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
log.addHandler(ch)
main()
Server output:
2021-05-01 20:55:47,394 DEBUG [selector_events:59] Using selector: KqueueSelector
2021-05-01 20:55:55,102 INFO [aioserver:26] New Connection
2021-05-01 20:56:05,107 WARNING [aioserver:44] Expected WORLD, received None
2021-05-01 20:56:05,107 INFO [aioserver:24] End Connection
Client:
def make_connection(host, port):
task = asyncio.Task(handle_client(host, port))
clients[task] = (host, port)
def client_done(task):
del clients[task]
log.info("Client Task Finished")
if len(clients) == 0:
log.info("clients is empty, stopping loop.")
loop = asyncio.get_event_loop()
loop.stop()
log.info("New Client Task")
task.add_done_callback(client_done)
async def handle_client(host, port):
log.info("Connecting to %s %d", host, port)
client_reader, client_writer = await asyncio.open_connection(host, port)
log.info("Connected to %s %d", host, port)
try:
data = struct.pack('HBBQ', 0, 255, 255, 255)
client_writer.write(data)
finally:
log.info("Disconnecting from %s %d", host, port)
client_writer.close()
log.info("Disconnected from %s %d", host, port)
def main():
log.info("MAIN begin")
loop = asyncio.get_event_loop()
make_connection('localhost', 8080)
loop.run_forever()
log.info("MAIN end")
if __name__ == '__main__':
log = logging.getLogger("")
formatter = logging.Formatter("%(asctime)s %(levelname)s " +
"[%(module)s:%(lineno)d] %(message)s")
# setup console logging
log.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
log.addHandler(ch)
main()
Anyway, does anyone know a faster python server framework?
Solution
Solved... There was no buffer size defined on server side. Instead of client_reader.readline()
it should be client_reader.read(128)
Answered By - Aqamarine
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.