OSError: [Errno 10048] error while attempting to bind on address

OSError: [Errno 10048] error while attempting to bind on address

OSError: [Errno 10048] error while attempting to bind on address ('0.0.0.0', 8080): only one usage of e
ach socket address (protocol/network address/port) is normally permitted

我已经安装了 aiohttp 并且如教程中所述, 我尝试 运行 脚本使用 python main.py 命令

from aiohttp import web
async def index(request):
    return web.Response(text='Hello Aiohttp!')
app = web.Application()
web.run_app(app)

我收到这个错误,不知道如何解决这个问题。

感谢任何形式的帮助

来自文档https://aiohttp.readthedocs.io/en/stable/web_reference.html#aiohttp.web.run_app。您可以将端口传递为

from aiohttp import web
async def index(request):
    return web.Response(text='Hello Aiohttp!')
app = web.Application()
web.run_app(app, port=9090)

您的问题是某些进程已经 运行 在 8080 端口号上。 解决问题的方法有两种

  1. sudo kill `sudo lsof -t -i:8080`(如果您正在处理 ubuntu)或 sudo kill $(sudo lsof -t -i:8080)

  2. python -m aiohttp.web -H localhost -P 5050 package.module.init_func

    package.module.init_func 应该是一个可导入的可调用对象,它接受任何未解析的命令行参数的列表和 returns 设置后的 Application 实例:

    def init_function(argv):
        app = web.Application()
        app.router.add_route("GET", "/", index_handler)
        return app
    

希望以上解决方案对您有所帮助。

您可以查看 aiohttp 的文档以了解更多信息。 https://aiohttp.readthedocs.io/en/v0.21.5/web.html