From 1e1f55c9e5d98ba076bc67e7abe9e4d77d84c65b Mon Sep 17 00:00:00 2001 From: James Taylor Date: Mon, 25 Feb 2019 20:47:49 -0800 Subject: Use persistent connections --- server.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'server.py') diff --git a/server.py b/server.py index 17da4cc..3f400a3 100644 --- a/server.py +++ b/server.py @@ -2,16 +2,19 @@ from gevent import monkey monkey.patch_all() import gevent.socket -from gevent.pywsgi import WSGIServer from youtube.youtube import youtube +from youtube import util import http_errors +import settings + +from gevent.pywsgi import WSGIServer import urllib +import urllib3 import socket import socks, sockshandler import subprocess import re -import settings @@ -31,15 +34,14 @@ def proxy_site(env, start_response): url += '?' + env['QUERY_STRING'] - req = urllib.request.Request(url, headers=headers) - if settings.route_tor: - opener = urllib.request.build_opener(sockshandler.SocksiPyHandler(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9150)) - response = opener.open(req, timeout=10) - else: - response = urllib.request.urlopen(req, timeout=10) + content, response = util.fetch_url(url, headers, return_response=True) + + headers = response.getheaders() + if isinstance(headers, urllib3._collections.HTTPHeaderDict): + headers = headers.items() - start_response('200 OK', response.getheaders() ) - return response.read() + start_response('200 OK', headers ) + return content site_handlers = { 'youtube.com':youtube, -- cgit v1.2.3