I am trying to build a reverse-proxy to talk to certain APIs(like Twitter, Github, Instagram) that I can then call with my reverse-proxy to any (client) applications I want (think of it like an API-manager).
Also, I am using an LXC-container to do this.
For example, here is the simplest of code that I hacked from the examples on the Twisted Docs:
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()
When I do CURL within the container, I get a valid request (meaning I get the appropriate JSON response).
Here is how I used the CURL command:
curl https://api.github.com/users/defunkt
And here is the output I get:
{
"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": "chris@github.com",
"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"
}
However, when I attempt fetching the proxy via Firefox using:
I get: "Could not connect"
This is what my Twisted log looks like:
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
How can I use Twisted to make an API call (most APIs are HTTPS nowadays anyway) and get the required response (basically, what the "200" response/JSON should be)?
I tried looking at this question: Convert HTTP Proxy to HTTPS Proxy in Twisted
But it didn't make much sense from a coding point-of-view (or mention anything about reverse-proxying).
**Edit: I also tried switching out the HTTPS API call for a regular HTTP call using:
curl http[colon][slash][slash]openlibrary[dot]org[slash]authors[slash]OL1A.json
(URL above has been formatted to avoid link-conflict issue)
However, I still get the same error in my browser (as mentioned above).
**Edit2: I have tried running your code, but I get this error:
If you look at the image, you will see the error (when running the code) of:
builtins.AttributeError: 'str' object has no attribute 'decode'