Search code examples
pythontornado

Tornado HTTP proxy server not catching HTTPS request


I have set up a tornado HTTP server which is working as a proxy server. I am using the python requests library to use it as a proxy server. When I try to fetch HTTP url's with it, it works fine. But it isn't intercepting HTTPS requests.

The proxy server part:

class ProxyServer(HTTPServerConnectionDelegate):
    def start_request(self, server_conn, request_conn):
        print('In start request')
        return ClientDelegator(request_conn)

    def on_close(self):
        pass

    def client_send_error(self):
        self.write('Error happened.')
        self.finish()


def main():
    server = HTTPServer(ProxyServer())
    server.bind(8888)
    server.start(0)
    tornado.ioloop.IOLoop.current().start()


if __name__ == "__main__":
    main()

The requests part:

import requests


url = 'https://example.com'
proxy = {'http' : '127.0.0.1:8888'}
r = requests.get(url, proxies=proxy, verify=False)
print(r.text)

When I use http://example.com, the connection starts as 'In start request' gets printed. However, when I use https://example.com then the connection doesn't start. The ProxyServer doesn't enter start_request.

What am I doing wrong?


Solution

  • Your proxy variable only specifies a proxy for http, not https. You need to set the proxy for both protocols separately.