Skip to content

Instantly share code, notes, and snippets.

@craigderington
Last active May 4, 2021 23:51
Show Gist options
  • Save craigderington/c78f28902941ce89081562bd82f00599 to your computer and use it in GitHub Desktop.
Save craigderington/c78f28902941ce89081562bd82f00599 to your computer and use it in GitHub Desktop.
Async Socket Server
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