当前位置:   article > 正文

Python3协程中socket的使用_create_datagram_endpoint

create_datagram_endpoint

Python3协程中socket的使用

TCP server

  1. import asyncio
  2. class EchoServerProtocol(asyncio.Protocol):
  3. def connection_made(self, transport):
  4. peername = transport.get_extra_info('peername')
  5. print('Connection from {}'.format(peername))
  6. self.transport = transport
  7. def data_received(self, data):
  8. message = data.decode()
  9. print('Data received: {!r}'.format(message))
  10. print('Send: {!r}'.format(message))
  11. self.transport.write(data)
  12. print('Close the client socket')
  13. self.transport.close()
  14. async def main():
  15. # Get a reference to the event loop as we plan to use
  16. # low-level APIs.
  17. loop = asyncio.get_running_loop()
  18. server = await loop.create_server(
  19. lambda: EchoServerProtocol(),
  20. '127.0.0.1', 8888)
  21. async with server:
  22. await server.serve_forever()
  23. asyncio.run(main())
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号