简体   繁体   中英

Python asyncio socket server eating errors (not displaying any errors)

Edited my question to provide more information and a reproducible example.

I'm not sure if anybody could help me here. But I'm having some problems with my asyncio socket server.

As you can see, in the server.py on line 18 there is a deliberate line that will cause a value error, as you cannot change "" to an int. This is intentional, as for some reason the error on this line is never output to the console.

When the client connects to the server the print("here1") line runs, but the lines after that do not due to the error on line 18. I need this error to be output into console, but it appears nowhere. I am very confused and cannot find anything online about why asyncio could be eating the errors and not displaying them.

I have attempted to see any errors by using the logging module, but even this doesn't show any errors..

import logging
logging.basicConfig(level=logging.ERROR)

The line which causes the error is definitely running.

I have extracted all of my code into a smaller reproducible set of files below.

server.py

import asyncio


class Server:
    def __init__(self, host: str, port: int):
        self.host = host
        self.port = port

    async def start_server(self):
        print("Server online")
        server = await asyncio.start_server(self.handle_events, self.host, self.port)
        async with server:
            await server.serve_forever()

    async def handle_events(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
        while True:
            print("here1")
            int("")
            print("here2")
            data = await reader.read(1024)
            if not data:
                continue
            print(f"received: {data}")


async def start():
    host = "127.0.0.1"
    port = 55551
    server = Server(host=host, port=port)
    server_task = asyncio.create_task(server.start_server())
    await asyncio.gather(server_task)

if __name__ == "__main__":
    asyncio.run(start())

client.py

import asyncio


class Client:
    def __init__(self, host: str, port: int):
        super().__init__()
        self.host = host
        self.port = port
        self.writer = None
        self.reader = None

    async def start_connection(self):
        self.reader, self.writer = await asyncio.open_connection(host=self.host, port=self.port)
        await self.message_handler()

    async def message_handler(self):
        while True:
            await asyncio.sleep(0)
            data = await self.reader.read(1024)
            if not data:
                continue

            await self.writer.drain()


async def start():
    host = "127.0.0.1"
    port = 55551
    server = Client(host=host, port=port)
    server_task = asyncio.create_task(server.start_connection())
    await asyncio.gather(server_task)

if __name__ == "__main__":
    asyncio.run(start())

Cause it's not even calling that handle_events func.

import asyncio

async def tcp_echo_client(message):
    reader, writer = await asyncio.open_connection(
        '127.0.0.1', 55555)

    print(f'Send: {message!r}')
    writer.write(message.encode())
    await writer.drain()

    data = await reader.read(100)
    print(f'Received: {data.decode()!r}')

    print('Close the connection')
    writer.close()

asyncio.run(tcp_echo_client('Hello World!'))

I just copied this from the doc so I can test it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM