赞
踩
TCP server
- import asyncio
-
-
- class EchoServerProtocol(asyncio.Protocol):
- def connection_made(self, transport):
- peername = transport.get_extra_info('peername')
- print('Connection from {}'.format(peername))
- self.transport = transport
-
- def data_received(self, data):
- message = data.decode()
- print('Data received: {!r}'.format(message))
-
- print('Send: {!r}'.format(message))
- self.transport.write(data)
-
- print('Close the client socket')
- self.transport.close()
-
-
- async def main():
- # Get a reference to the event loop as we plan to use
- # low-level APIs.
- loop = asyncio.get_running_loop()
-
- server = await loop.create_server(
- lambda: EchoServerProtocol(),
- '127.0.0.1', 8888)
-
- async with server:
- await server.serve_forever()
-
-
- asyncio.run(main())
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。