简体   繁体   English

客户端和aiohttp Web服务器之间的短路连接

[英]Short circuit connection between client and aiohttp web server

I'm writing an application that use aiohttp to publish a web api. 我正在编写一个使用aiohttp发布web api的应用程序。 I also need to publish the same api with another protocol (xmpp). 我还需要使用另一个协议(xmpp)发布相同的api。 Currently the solution is to embed an xmpp client in the same process that connects to the web api using a regular HTTP connection (aiohttp client) and forwards the response through xmpp. 目前解决方案是将xmpp客户端嵌入到使用常规HTTP连接(aiohttp客户端)连接到web api的相同进程中,并通过xmpp转发响应。 But since they are both running in the same process I would prefer if there was a way to "short circuit" the connection. 但是因为它们都在同一个过程中运行,所以如果有一种方法可以“短路”连接,我更愿意。

I'm thinking something like creating a Request instance and passing it to a method in the aiohttp web application (perhaps the _handle() method?) to process it. 我正在考虑创建一个Request实例并将其传递给aiohttp Web应用程序中的方法(可能是_handle()方法?)来处理它。

Any hints on how to do this in a (preferably) non-hackish manner? 关于如何以(最好)非hackish方式做到这一点的任何提示?

I have something working now that at least does not depend on private methods: 我现在有一些工作,至少不依赖于私有方法:

req = Request(...)
req.match_info = await app.router.resolve("/api/1.0/status")
response = req.match_info.handler(req)

This works, but there's room for improvements. 这有效,但还有改进的余地。 I use a custom, simplified Request class as well: 我也使用自定义的简化Request类:

from yarl import URL

class XmppHttpRequest:
    def __init__(self, method, path):
        self.method = method
        self.path = path
        self.rel_url = URL(path)
        self.url = self.rel_url
        self.match_info = None

This is all I require for now. 这就是我现在所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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