Add magic check
This commit is contained in:
parent
e227d349b2
commit
461fc7ce0b
17
server.py
17
server.py
|
@ -4,19 +4,32 @@ import logging
|
||||||
|
|
||||||
LOGGER = logging.getLogger("server")
|
LOGGER = logging.getLogger("server")
|
||||||
|
|
||||||
|
MAGIC = "e8437140-4347-48cc-a31d-dcdc944ffc16"
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
asyncio.run(run())
|
asyncio.run(run())
|
||||||
|
|
||||||
async def on_connect(reader, writer):
|
async def on_connect(reader, writer):
|
||||||
LOGGER.info("connected")
|
LOGGER.info("connected")
|
||||||
data = await reader.read()
|
data = await reader.read(36)
|
||||||
print(data.decode("UTF-8"))
|
magic = data.decode("UTF-8")
|
||||||
|
LOGGER.debug("Received magic '%s'", magic)
|
||||||
|
if magic != MAGIC:
|
||||||
|
writer.write("ERR1: Magic not recognized".encode("UTF-8"))
|
||||||
|
writer.close()
|
||||||
|
LOGGER.info("Bad magic, closing connection.")
|
||||||
|
return
|
||||||
|
LOGGER.debug("Magic looks good.")
|
||||||
|
|
||||||
|
|
||||||
async def run():
|
async def run():
|
||||||
server = await asyncio.start_server(on_connect, host="localhost", port=9988)
|
server = await asyncio.start_server(on_connect, host="localhost", port=9988)
|
||||||
async with server:
|
async with server:
|
||||||
|
try:
|
||||||
await server.serve_forever()
|
await server.serve_forever()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
LOGGER.info("Exiting at user request.")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue