aioudp
v1.0.1
更好的非同步 UDP API
類似 websockets 的 UDP API
這是一個回顯伺服器範例:
import asyncio
import signal
import aioudp
async def main ():
async def handler ( connection ):
async for message in connection :
await connection . send ( message )
# Optional. This is for properly exiting the server when Ctrl-C is pressed
# or when the process is killed/terminated
loop = asyncio . get_running_loop ()
stop = loop . create_future ()
loop . add_signal_handler ( signal . SIGTERM , stop . set_result , None )
loop . add_signal_handler ( signal . SIGINT , stop . set_result , None )
# Serve the server
async with aioudp . serve ( "localhost" , 9999 , handler ):
await stop # Serve forever
if __name__ == '__main__' :
asyncio . run ( main ())
以及連接到伺服器的客戶端:
import asyncio
import aioudp
async def main ():
async with aioudp . connect ( "localhost" , 9999 ) as connection :
await connection . send ( b"Hello world!" )
assert await connection . recv () == b"Hello world!"
if __name__ == '__main__' :
asyncio . run ( main ())
您可以透過pip
取得該項目
$ pip install aioudp
或者,如果你使用詩歌
$ poetry add aioudp
筆記
除了基於async
/ await
的 API 之外,該程式庫不提供對asyncio
中現有 UDP 介面的其他抽象。這意味著該庫中沒有處理隱式協議,例如 QUIC。您必須自己編寫,或者尋找另一個庫。
async
IO 實作。它有一個類似的 API(在我寫這個庫之前我並不知道)版權所有 © 2021,布萊恩胡
該專案已獲得 GNU GPL v3+ 的許可。
簡而言之,這意味著您可以用它做任何事情(分發、修改、銷售),但如果您要發布更改,則必須隨時提供原始程式碼和建置說明。
如果您是使用該專案的公司並希望獲得例外,請發送電子郵件至 [email protected],我們可以進行討論。