2020-11-17 10:54:18 +01:00
|
|
|
import giambio
|
|
|
|
from giambio.socket import AsyncSocket
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
|
|
|
|
# A test to check for asynchronous I/O
|
|
|
|
|
|
|
|
|
2020-12-20 15:58:53 +01:00
|
|
|
async def serve(bind_address: tuple):
|
|
|
|
"""
|
|
|
|
Serves asynchronously forever
|
|
|
|
|
|
|
|
:param bind_address: The address to bind the server to represented as a tuple
|
|
|
|
(address, port) where address is a string and port is an integer
|
|
|
|
"""
|
|
|
|
|
2021-04-23 09:17:55 +02:00
|
|
|
sock = giambio.socket.socket()
|
|
|
|
await sock.bind(bind_address)
|
|
|
|
await sock.listen(5)
|
2020-12-20 15:58:53 +01:00
|
|
|
logging.info(f"Serving asynchronously at {bind_address[0]}:{bind_address[1]}")
|
2020-11-17 10:54:18 +01:00
|
|
|
async with giambio.create_pool() as pool:
|
2021-08-26 16:54:13 +02:00
|
|
|
async with sock:
|
2022-02-05 16:14:21 +01:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
conn, address_tuple = await sock.accept()
|
|
|
|
logging.info(f"{address_tuple[0]}:{address_tuple[1]} connected")
|
|
|
|
await pool.spawn(handler, conn, address_tuple)
|
|
|
|
except Exception as err:
|
|
|
|
# Because exceptions just *work*
|
|
|
|
logging.info(f"{address_tuple[0]}:{address_tuple[1]} has raised {type(err).__name__}: {err}")
|
2020-12-20 15:58:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def handler(sock: AsyncSocket, client_address: tuple):
|
|
|
|
"""
|
|
|
|
Handles a single client connection
|
2020-11-18 12:13:46 +01:00
|
|
|
|
2021-07-22 11:13:08 +02:00
|
|
|
:param sock: The AsyncSocket object connected to the client
|
2020-12-20 15:58:53 +01:00
|
|
|
:param client_address: The client's address represented as a tuple
|
|
|
|
(address, port) where address is a string and port is an integer
|
|
|
|
:type client_address: tuple
|
|
|
|
"""
|
2020-11-17 10:54:18 +01:00
|
|
|
|
2020-12-20 15:58:53 +01:00
|
|
|
address = f"{client_address[0]}:{client_address[1]}"
|
2021-06-08 17:21:59 +02:00
|
|
|
async with sock: # Closes the socket automatically
|
2021-07-22 11:13:08 +02:00
|
|
|
await sock.send_all(b"Welcome to the server pal, feel free to send me something!\n")
|
2020-11-17 10:54:18 +01:00
|
|
|
while True:
|
|
|
|
await sock.send_all(b"-> ")
|
|
|
|
data = await sock.receive(1024)
|
|
|
|
if not data:
|
|
|
|
break
|
2020-12-20 15:58:53 +01:00
|
|
|
elif data == b"exit\n":
|
2020-11-17 10:54:18 +01:00
|
|
|
await sock.send_all(b"I'm dead dude\n")
|
2021-08-28 23:26:24 +02:00
|
|
|
raise TypeError("Oh, no, I'm gonna die!")
|
2020-12-20 15:58:53 +01:00
|
|
|
logging.info(f"Got: {data!r} from {address}")
|
|
|
|
await sock.send_all(b"Got: " + data)
|
|
|
|
logging.info(f"Echoed back {data!r} to {address}")
|
2020-12-19 15:18:12 +01:00
|
|
|
logging.info(f"Connection from {address} closed")
|
2020-11-17 10:54:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-04-22 12:02:40 +02:00
|
|
|
port = int(sys.argv[1]) if len(sys.argv) > 1 else 1501
|
2021-06-08 17:21:59 +02:00
|
|
|
logging.basicConfig(
|
|
|
|
level=20,
|
|
|
|
format="[%(levelname)s] %(asctime)s %(message)s",
|
|
|
|
datefmt="%d/%m/%Y %p",
|
|
|
|
)
|
2020-11-17 10:54:18 +01:00
|
|
|
try:
|
2021-04-23 09:17:55 +02:00
|
|
|
giambio.run(serve, ("localhost", port))
|
2020-11-17 10:54:18 +01:00
|
|
|
except (Exception, KeyboardInterrupt) as error: # Exceptions propagate!
|
|
|
|
if isinstance(error, KeyboardInterrupt):
|
|
|
|
logging.info("Ctrl+C detected, exiting")
|
|
|
|
else:
|
|
|
|
logging.error(f"Exiting due to a {type(error).__name__}: {error}")
|