mirror of
https://github.com/CopterExpress/clever-show.git
synced 2026-05-28 16:09:33 +00:00
Request\response processing
This commit is contained in:
44
examples/code/connection.py
Normal file
44
examples/code/connection.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
import lib.connections as c
|
||||
import lib.messages as messages
|
||||
import lib.utils as utils
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.DEBUG,
|
||||
format=utils.logger_format,
|
||||
handlers=[
|
||||
logging.StreamHandler()
|
||||
])
|
||||
|
||||
async def server():
|
||||
server, new_connections = await c.create_server(8181)
|
||||
connection1: c.Connection = await new_connections
|
||||
#await connection1.send(messages.Request("greetings"))
|
||||
await asyncio.sleep(2)
|
||||
t1 = asyncio.create_task(asyncio.wait_for(connection1.send(messages.Request("greetings")), 5))
|
||||
t2 = asyncio.create_task(connection1.send(messages.Request("hello", args=(1, 3))))
|
||||
|
||||
print(await asyncio.gather(t1, t2, return_exceptions=True))
|
||||
await connection1.close()
|
||||
server.close()
|
||||
await server.wait_closed()
|
||||
|
||||
async def client():
|
||||
connection = await c.connect(8181, "")
|
||||
request = await connection.receive("hello")
|
||||
print(request.message.content)
|
||||
request2 = await connection.receive()
|
||||
print(request2.message.content)
|
||||
|
||||
await request.reply("hi")
|
||||
|
||||
# await connection.close()
|
||||
|
||||
async def main():
|
||||
s = asyncio.create_task(server())
|
||||
c = asyncio.create_task(client())
|
||||
await asyncio.gather(s, c)
|
||||
|
||||
asyncio.run(main(), debug=False)
|
||||
@@ -13,9 +13,25 @@ logging.basicConfig(
|
||||
|
||||
async def server():
|
||||
loop = asyncio.get_event_loop()
|
||||
clients = asyncio.Queue()
|
||||
|
||||
server = await loop.create_server(lambda: p.PeerProtocol(None, None), host='', port=8181)
|
||||
def factory(*args):
|
||||
protocol = p.PeerProtocol()
|
||||
clients.put_nowait(protocol)
|
||||
return protocol
|
||||
|
||||
server = await loop.create_server(factory, host='', port=8181)
|
||||
|
||||
client1 = await clients.get()
|
||||
await client1.connected
|
||||
msg = await client1.receive_message()
|
||||
print(msg.header, msg.content)
|
||||
|
||||
msg = messages.PendingMessage(msg.content + " from server")
|
||||
await msg.send_to(client1)
|
||||
|
||||
await client1.closed
|
||||
server.close()
|
||||
await server.wait_closed()
|
||||
|
||||
async def client():
|
||||
@@ -23,9 +39,11 @@ async def client():
|
||||
|
||||
transport, protocol = await loop.create_connection(lambda: p.PeerProtocol(None, None),
|
||||
host='', port=8181)
|
||||
await protocol.send(messages.Request("greetings"))
|
||||
|
||||
await protocol.closed
|
||||
await protocol.send(messages.PendingMessage("greetings"))
|
||||
print((await protocol.receive_message()).content)
|
||||
|
||||
await protocol.close()
|
||||
|
||||
async def main():
|
||||
s = asyncio.create_task(server())
|
||||
|
||||
Reference in New Issue
Block a user