Commit a8bd9f45 authored by Alan Marchiori's avatar Alan Marchiori

comments

parent 63e95db6
...@@ -54,7 +54,7 @@ def main(addr, port): ...@@ -54,7 +54,7 @@ def main(addr, port):
#while True: #while True:
if True: if True:
#data, addr = skt.recvfrom(1024) # blocking #data, addr = skt.recvfrom(1024) # blocking
data = clientsocket.recv(1024) # blocking, note not recvfrom! data = clientsocket.recv(1024) # blocking, note *not* recvfrom!
if data: if data:
handle_req(clientsocket, handle_req(clientsocket,
data, data,
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
Asyncio udp echo/caps server Asyncio udp echo/caps server
Alan Marchiori 2019 Alan Marchiori 2019
May be different in python >3.6, requires python >3.5 May be different in python >3.6, requires python >=3.5
""" """
import logging import logging
import asyncio # note not socket or socketserver! import asyncio # note not socket or socketserver!
...@@ -16,7 +16,7 @@ class MyUdpProtocol: ...@@ -16,7 +16,7 @@ class MyUdpProtocol:
self.loop = loop self.loop = loop
self.server_address = server_address # just for printing self.server_address = server_address # just for printing
def connection_made(self, transport): def connection_made(self, transport):
self.transport = transport self.transport = transport
self.log.info("connection_made: {}".format( self.log.info("connection_made: {}".format(
...@@ -28,6 +28,10 @@ class MyUdpProtocol: ...@@ -28,6 +28,10 @@ class MyUdpProtocol:
async def handle_response(self, data, addr): async def handle_response(self, data, addr):
"simulate a long running response method" "simulate a long running response method"
# log rx.
log.info("{} -> {}: {}".format(
addr, self.server_address, data))
# processing delay # processing delay
await asyncio.sleep(3) await asyncio.sleep(3)
...@@ -46,10 +50,8 @@ class MyUdpProtocol: ...@@ -46,10 +50,8 @@ class MyUdpProtocol:
a long running task to handle the request. a long running task to handle the request.
""" """
# log rx.
log.info("{} -> {}: {}".format(
addr, self.server_address, data))
# create and schedule the task defined by the handle_response coroutine
self.loop.create_task(self.handle_response(data, addr)) self.loop.create_task(self.handle_response(data, addr))
if __name__ == "__main__": if __name__ == "__main__":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment