diff options
| author | Ratan Kulshreshtha <ratan.shreshtha@gmail.com> | 2019-05-25 03:05:50 +0530 |
|---|---|---|
| committer | Ratan Kulshreshtha <ratan.shreshtha@gmail.com> | 2019-05-25 03:05:50 +0530 |
| commit | 266b347d393a16684f2b5abb87aeb5b13ca4f0b7 (patch) | |
| tree | e8a810cfaa647f61027f82175b29cae2ceb2a61e /dummyserver/proxy.py | |
| parent | 3773cf123cf1e195691f26ad870de8f6b423e7a9 (diff) | |
| download | urllib3-266b347d393a16684f2b5abb87aeb5b13ca4f0b7.tar.gz | |
Auto formatting using black
Diffstat (limited to 'dummyserver/proxy.py')
| -rwxr-xr-x | dummyserver/proxy.py | 44 |
1 files changed, 25 insertions, 19 deletions
diff --git a/dummyserver/proxy.py b/dummyserver/proxy.py index 907dd074..7429d441 100755 --- a/dummyserver/proxy.py +++ b/dummyserver/proxy.py @@ -34,25 +34,30 @@ import tornado.iostream import tornado.web import tornado.httpclient -__all__ = ['ProxyHandler', 'run_proxy'] +__all__ = ["ProxyHandler", "run_proxy"] class ProxyHandler(tornado.web.RequestHandler): - SUPPORTED_METHODS = ['GET', 'POST', 'CONNECT'] + SUPPORTED_METHODS = ["GET", "POST", "CONNECT"] @tornado.web.asynchronous def get(self): - def handle_response(response): - if response.error and not isinstance(response.error, - tornado.httpclient.HTTPError): + if response.error and not isinstance( + response.error, tornado.httpclient.HTTPError + ): self.set_status(500) - self.write('Internal server error:\n' + str(response.error)) + self.write("Internal server error:\n" + str(response.error)) self.finish() else: self.set_status(response.code) - for header in ('Date', 'Cache-Control', 'Server', - 'Content-Type', 'Location'): + for header in ( + "Date", + "Cache-Control", + "Server", + "Content-Type", + "Location", + ): v = response.headers.get(header) if v: self.set_header(header, v) @@ -62,19 +67,22 @@ class ProxyHandler(tornado.web.RequestHandler): req = tornado.httpclient.HTTPRequest( url=self.request.uri, - method=self.request.method, body=self.request.body, - headers=self.request.headers, follow_redirects=False, - allow_nonstandard_methods=True) + method=self.request.method, + body=self.request.body, + headers=self.request.headers, + follow_redirects=False, + allow_nonstandard_methods=True, + ) client = tornado.httpclient.AsyncHTTPClient() try: client.fetch(req, handle_response) except tornado.httpclient.HTTPError as e: - if hasattr(e, 'response') and e.response: + if hasattr(e, "response") and e.response: self.handle_response(e.response) else: self.set_status(500) - self.write('Internal server error:\n' + str(e)) + self.write("Internal server error:\n" + str(e)) self.finish() @tornado.web.asynchronous @@ -83,7 +91,7 @@ class ProxyHandler(tornado.web.RequestHandler): @tornado.web.asynchronous def connect(self): - host, port = self.request.uri.split(':') + host, port = self.request.uri.split(":") client = self.request.connection.stream def read_from_client(data): @@ -109,7 +117,7 @@ class ProxyHandler(tornado.web.RequestHandler): def start_tunnel(): client.read_until_close(client_close, read_from_client) upstream.read_until_close(upstream_close, read_from_upstream) - client.write(b'HTTP/1.0 200 Connection established\r\n\r\n') + client.write(b"HTTP/1.0 200 Connection established\r\n\r\n") s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) upstream = tornado.iostream.IOStream(s) @@ -121,16 +129,14 @@ def run_proxy(port, start_ioloop=True): Run proxy on the specified port. If start_ioloop is True (default), the tornado IOLoop will be started immediately. """ - app = tornado.web.Application([ - (r'.*', ProxyHandler), - ]) + app = tornado.web.Application([(r".*", ProxyHandler)]) app.listen(port) ioloop = tornado.ioloop.IOLoop.instance() if start_ioloop: ioloop.start() -if __name__ == '__main__': +if __name__ == "__main__": port = 8888 if len(sys.argv) > 1: port = int(sys.argv[1]) |
