Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import binascii
- import struct
- import asyncio
- TCP_IP = '127.0.0.1'
- TCP_PORT = 30194
- BUFFER_SIZE = 1024
- loop = asyncio.get_event_loop()
- coro = asyncio.start_server(incomingData, TCP_IP, TCP_PORT)
- server = loop.run_until_complete(coro)
- try:
- loop.run_forever()
- except KeyboardInterrupt:
- pass
- # Async method to handle incoming connections
- async def incomingData(reader, writer):
- data = await reader.read(BUFFER_SIZE)
- incomingMessage = data.decode()
- addr = writer.get_extra_info('peername')
- print("Received %r from %r" % (incomingMessage, addr))
- # [0] login packet
- if ord(incomingMessage[0]) == 0:
- username = ""
- password = ""
- try:
- usernameLength = ord(incomingMessage[1]) + 2 # plus two for the 2 chars taken up by the hex
- passwordLength = ord(incomingMessage[usernameLength]) + 1 + usernameLength
- for x in range(2,usernameLength):
- username += incomingMessage[x]
- for i in range(usernameLength,passwordLength):
- password += incomingMessage[i]
- loginHandler.username = username
- loginHandler.password = password
- except:
- print("Unexpected error")
- writer.write(data)
- await writer.drain()
- writer.close()
- try:
- while True:
- print("debug print?")
- loop.run_forever()
Add Comment
Please, Sign In to add comment