Last active
May 4, 2021 23:51
-
-
Save craigderington/c78f28902941ce89081562bd82f00599 to your computer and use it in GitHub Desktop.
Async Socket Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio, socket | |
async def handle_client(reader, writer): | |
request = None | |
while request != 'quit': | |
request = (await reader.read(255)).decode('utf8') | |
response = str(eval(request)) + '\n' | |
writer.write(response.encode('utf8')) | |
await writer.drain() | |
writer.close() | |
async def run_server(): | |
server = await asyncio.start_server(handle_client, 'localhost', 15555) | |
async with server: | |
await server.serve_forever() | |
asyncio.run(run_server()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment