我正在尝试构建一个反向代理以与某些API(如Twitter,Github,Instagram)进行对话,然后我可以使用反向代理与任何API进行通信(客户端)应用程序(将其想像为API管理器)。Python-Twisted:反向代理到HTTPS API:无法连接
此外,我正在使用LXC容器来做到这一点。
例如,这里是最简单的,我从在被扭曲文档的例子砍死代码:
from twisted.internet import reactor
from twisted.web import proxy, server
from twisted.python.log import startLogging
from sys import stdout
startLogging(stdout)
site = server.Site(proxy.ReverseProxyResource('https://api.github.com/users/defunkt', 443, b''))
reactor.listenTCP(8080, site)
reactor.run()
当我做容器内卷曲,我得到一个有效的请求(这意味着我得到适当的JSON响应)。
这是我如何使用curl命令:
curl https://api.github.com/users/defunkt
,这里是输出我得到:
{
"login": "defunkt",
"id": 2,
"avatar_url": "https://avatars.githubusercontent.com/u/2?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/defunkt",
"html_url": "https://github.com/defunkt",
"followers_url": "https://api.github.com/users/defunkt/followers",
"following_url": "https://api.github.com/users/defunkt/following{/other_user}",
"gists_url": "https://api.github.com/users/defunkt/gists{/gist_id}",
"starred_url": "https://api.github.com/users/defunkt/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/defunkt/subscriptions",
"organizations_url": "https://api.github.com/users/defunkt/orgs",
"repos_url": "https://api.github.com/users/defunkt/repos",
"events_url": "https://api.github.com/users/defunkt/events{/privacy}",
"received_events_url": "https://api.github.com/users/defunkt/received_events",
"type": "User",
"site_admin": true,
"name": "Chris Wanstrath",
"company": "GitHub",
"blog": "http://chriswanstrath.com/",
"location": "San Francisco",
"email": "[email protected]",
"hireable": true,
"bio": null,
"public_repos": 107,
"public_gists": 280,
"followers": 15153,
"following": 208,
"created_at": "2007-10-20T05:24:19Z",
"updated_at": "2016-02-26T22:34:27Z"
}
然而,当我尝试通过Firefox中使用获取代理:
我得到:“无法连接”
这是我扭日志是什么样子:
2016-02-27 [-] Log opened.
2016-02-27 [-] Site starting on 8080
2016-02-27 [-] Starting factory
2016-02-27 [-] Starting factory
2016-02-27 [-] "10.5.5.225" - - [27/Feb/2016: +0000] "GET/HTTP/1.1" 501 26 "-" "Mozilla/5.0 (X11; Debian; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0"
2016-02-27 [-] Stopping factory
如何使用Twisted来进行API调用(大多数API是HTTPS现在反正),并得到所需要的反应(基本上,什么是“200”响应/ JSON应该是)?
我试图寻找这个问题:Convert HTTP Proxy to HTTPS Proxy in Twisted
但它并没有从编码点的视图多大意义(或提及关于反向代理的任何东西)。
**编辑:我也尝试过使用切换了HTTPS API调用一个普通HTTP调用:
curl http[colon][slash][slash]openlibrary[dot]org[slash]authors[slash]OL1A.json
(URL上面已被格式化,以避免链路冲突的问题)
然而,我的浏览器仍然出现同样的错误(如上所述)。
** EDIT2:我已经尝试运行你的代码,但我得到这个错误:
如果你看一下图片,你会看到错误的(当运行代码时):
builtins.AttributeError: 'str' object has no attribute 'decode'
当我运行这个例子时,'curl'和Firefox都会说“无法连接”,所以我不知道你在做什么来获得正确的JSON响应。你是否按照书面形式运行代码示例? – Glyph
哇,你是Twisted的创始人,很高兴认识你,先生!我正在运行代码:'python3 file.py'。就输出而言,我正在编辑我的问题,以便说明如何使用卷曲和输出。这可能是因为你被github API限制了速度(没有API密钥的公共调用有一些限制),但是我成功设法获得了JSON响应。 – coolpy
也很高兴见到你。感谢您使用Twisted :)。现在你已经明确了你如何运行你的命令,我可以回答它... – Glyph