1. <small id='dBFmu'></small><noframes id='dBFmu'>

      • <bdo id='dBFmu'></bdo><ul id='dBFmu'></ul>
    2. <legend id='dBFmu'><style id='dBFmu'><dir id='dBFmu'><q id='dBFmu'></q></dir></style></legend>
    3. <tfoot id='dBFmu'></tfoot>
      <i id='dBFmu'><tr id='dBFmu'><dt id='dBFmu'><q id='dBFmu'><span id='dBFmu'><b id='dBFmu'><form id='dBFmu'><ins id='dBFmu'></ins><ul id='dBFmu'></ul><sub id='dBFmu'></sub></form><legend id='dBFmu'></legend><bdo id='dBFmu'><pre id='dBFmu'><center id='dBFmu'></center></pre></bdo></b><th id='dBFmu'></th></span></q></dt></tr></i><div id='dBFmu'><tfoot id='dBFmu'></tfoot><dl id='dBFmu'><fieldset id='dBFmu'></fieldset></dl></div>

    4. Discord-py Rewrite - Cog 中的基本 aiohttp 网络服务器

      时间:2023-10-11
      • <i id='4T14Y'><tr id='4T14Y'><dt id='4T14Y'><q id='4T14Y'><span id='4T14Y'><b id='4T14Y'><form id='4T14Y'><ins id='4T14Y'></ins><ul id='4T14Y'></ul><sub id='4T14Y'></sub></form><legend id='4T14Y'></legend><bdo id='4T14Y'><pre id='4T14Y'><center id='4T14Y'></center></pre></bdo></b><th id='4T14Y'></th></span></q></dt></tr></i><div id='4T14Y'><tfoot id='4T14Y'></tfoot><dl id='4T14Y'><fieldset id='4T14Y'></fieldset></dl></div>

          <tbody id='4T14Y'></tbody>

        1. <legend id='4T14Y'><style id='4T14Y'><dir id='4T14Y'><q id='4T14Y'></q></dir></style></legend>

          <small id='4T14Y'></small><noframes id='4T14Y'>

          • <tfoot id='4T14Y'></tfoot>
            • <bdo id='4T14Y'></bdo><ul id='4T14Y'></ul>

                本文介绍了Discord-py Rewrite - Cog 中的基本 aiohttp 网络服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试在 Cog 中集成一个基本的 aiohttp 网络服务器(使用 discord-py 重写).我正在为 cog 使用以下代码:

                I am trying to integrate a basic aiohttp webserver in a Cog (using discord-py rewrite). I am using the following code for the cog:

                from aiohttp import web
                import discord
                from discord.ext import commands
                
                class Youtube():
                
                    def __init__(self, bot):
                        self.bot = bot
                
                    async def webserver(self):
                        async def handler(request):
                            return web.Response(text="Hello, world")
                
                        app = web.Application()
                        app.router.add_get('/', handler)
                        runner = web.AppRunner(app)
                        await runner.setup()
                        site = web.TCPSite(runner, '192.168.1.111', 8999)
                        await self.bot.wait_until_ready()
                        await site.start()
                
                def setup(bot):
                    yt = Youtube(bot)
                    bot.add_cog(yt)
                    bot.loop.create_task(yt.webserver())
                

                它在启动机器人时工作正常.但是如果我在机器人运行时重新加载 cog,我会遇到一个问题:

                It works fine upon starting the bot. But if I reload the cog while the bot is running, I encounter an issue:

                OSError: [Errno 10048] 尝试绑定地址时出错('192.168.1.111', 8999):每个socket地址只使用一次(协议/网络地址/端口)通常是允许的

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

                每次重新加载 cog 时,我都想不出一种简单/优雅的方式来释放和重新绑定.
                我很想对此提出一些建议.最终目标是拥有一个支持 youtube pubsubhubbub 订阅的 cog.

                I cannot think of an simple/elegant way to release and re bind every time the cog is reloaded.
                I would love some suggestions on this. The end goal is to have a cog that supports youtube pubsubhubbub subscriptions.

                可能只是有一种更好的方法可以将基本的网络服务器集成到我的机器人中.例如,我可以在启动机器人时使用一个守护进程(fork)(我已经有一个使用 HTTPServer 编写的网络服务器和一个可以处理 pubsubhubbub youtube 订阅的 BaseHTTPRequestHandler),但不知何故,我决定使用 aiohttp 将它集成到一个 cog 中:)

                It might just be that there is a better way to integrate a basic webserver to my bot. I could use a deamon (fork) upon starting the bot for example (I already have a webserver written using HTTPServer with a BaseHTTPRequestHandler that can handle pubsubhubbub youtube subscriptions) but somehow I have my mind set on integrating it in a cog using aiohttp :)

                推荐答案

                    from aiohttp import web
                    import asyncio
                    import discord 
                    from discord.ext import commands
                        
                        class Youtube():
                        
                            def __init__(self, bot):
                                self.bot = bot
                        
                            async def webserver(self):
                                async def handler(request):
                                    return web.Response(text="Hello, world")
                        
                                app = web.Application()
                                app.router.add_get('/', handler)
                                runner = web.AppRunner(app)
                                await runner.setup()
                                self.site = web.TCPSite(runner, '192.168.1.111', 8999)
                                await self.bot.wait_until_ready()
                                await self.site.start()
                    
                            def __unload(self):
                                asyncio.ensure_future(self.site.stop())
                        
                        def setup(bot):
                            yt = Youtube(bot)
                            bot.add_cog(yt)
                            bot.loop.create_task(yt.webserver())
                

                谢谢帕特里克·豪!!

                这篇关于Discord-py Rewrite - Cog 中的基本 aiohttp 网络服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:ValueError:replit 数据库检测到循环引用 下一篇:Discord.py 检查用户是否对特定的表情符号做出反应

                相关文章

                最新文章

                <legend id='t70Nt'><style id='t70Nt'><dir id='t70Nt'><q id='t70Nt'></q></dir></style></legend><tfoot id='t70Nt'></tfoot>

                1. <small id='t70Nt'></small><noframes id='t70Nt'>

                    <i id='t70Nt'><tr id='t70Nt'><dt id='t70Nt'><q id='t70Nt'><span id='t70Nt'><b id='t70Nt'><form id='t70Nt'><ins id='t70Nt'></ins><ul id='t70Nt'></ul><sub id='t70Nt'></sub></form><legend id='t70Nt'></legend><bdo id='t70Nt'><pre id='t70Nt'><center id='t70Nt'></center></pre></bdo></b><th id='t70Nt'></th></span></q></dt></tr></i><div id='t70Nt'><tfoot id='t70Nt'></tfoot><dl id='t70Nt'><fieldset id='t70Nt'></fieldset></dl></div>
                      <bdo id='t70Nt'></bdo><ul id='t70Nt'></ul>