
API ที่ดีกว่าสำหรับ UDP แบบอะซิงโครนัส
API ที่คล้ายกับ websockets สำหรับ UDP
นี่คือตัวอย่างเซิร์ฟเวอร์ echo:
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บันทึก
ไลบรารีนี้ไม่มีนามธรรมอื่นๆ เหนืออินเทอร์เฟซ UDP ที่มีอยู่ใน asyncio นอกเหนือจาก API แบบ async / await ซึ่งหมายความว่าไม่มีการจัดการโปรโตคอลโดยนัยในไลบรารีนี้ เช่น QUIC คุณต้องเขียนเองหรือหาห้องสมุดอื่น
async IO ใดๆ มี API ที่คล้ายกัน (ซึ่งฉันไม่รู้ก่อนที่จะเขียนไลบรารีนี้)ลิขสิทธิ์ © 2021, ไบรอัน หู
โครงการนี้ได้รับอนุญาตภายใต้ GNU GPL v3+
กล่าวโดยสรุป หมายความว่าคุณสามารถทำอะไรก็ได้ (แจกจ่าย แก้ไข ขาย) แต่ถ้าคุณต้องการเผยแพร่การเปลี่ยนแปลง คุณต้องทำให้ซอร์สโค้ดและคำแนะนำในการสร้างพร้อมใช้งาน
หากคุณเป็นบริษัทที่ใช้โครงการนี้และต้องการข้อยกเว้น โปรดส่งอีเมลถึงฉันที่ [email protected] แล้วเราจะพูดคุยกัน