mirror of
https://github.com/CopterExpress/clever-show.git
synced 2026-05-30 00:39:32 +00:00
refactored communication, added example
This commit is contained in:
35
examples/code/protocol.py
Normal file
35
examples/code/protocol.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
import lib.protocols as p
|
||||
import lib.messages as messages
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.DEBUG,
|
||||
format="%(asctime)s [%(name)-7.7s] [%(threadName)-19.19s] [%(levelname)-7.7s] %(message)s",
|
||||
handlers=[
|
||||
logging.StreamHandler()
|
||||
])
|
||||
|
||||
async def server():
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
server = await loop.create_server(lambda: p.PeerProtocol(None, None), host='', port=8181)
|
||||
|
||||
await server.wait_closed()
|
||||
|
||||
async def client():
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
transport, protocol = await loop.create_connection(lambda: p.PeerProtocol(None, None),
|
||||
host='', port=8181)
|
||||
await protocol.send(messages.Request("greetings"))
|
||||
|
||||
await protocol.closed
|
||||
|
||||
async def main():
|
||||
s = asyncio.create_task(server())
|
||||
c = asyncio.create_task(client())
|
||||
await asyncio.gather(s, c)
|
||||
|
||||
asyncio.run(main(), debug=True)
|
||||
Reference in New Issue
Block a user