Skip to Content
Python 技術手冊 第三版
book

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate to advanced content levelIntermediate to advanced
856 pages
17h 17m
Chinese
GoTop Information, Inc.
Content preview from Python 技術手冊 第三版
602
|
18 章 非同步的替代方案
import asyncio
class EchoServer(asyncio.Protocol):
def connection_made(self, transport):
self.transport = transport
self.peer = transport.get_extra_info('peername')
print('Connected from', self.peer)
def connection_lost(self, exc):
print('Disconnected from', self.peer)
def data_received(self, data):
print('Recv:', data.decode())
self.transport.write(data)
print('Echo:', data.decode())
loop = asyncio.get_event_loop()
echo = loop.create_server(EchoServer, 'localhost', 8881)
server = loop.run_until_complete(echo)
print('Serving at', server.sockets[0].getsockname())
loop.run_forever()
這個伺服器並沒有限制它一次可以服務多少客戶端,而其傳輸能處理任何
網路資料切割方式(network ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

測試驅動開發|使用 Python

測試驅動開發|使用 Python

Harry J.W. Percival
Linux 内核观测技术BPF

Linux 内核观测技术BPF

David Calavera, Lorenzo Fontana

Publisher Resources

ISBN: 9789864766819