فهرست منبع

HUE-8737 [core] Futurize apps/proxy for Python 3.5

Ying Chen 6 سال پیش
والد
کامیت
01b26f530a
2فایلهای تغییر یافته به همراه27 افزوده شده و 14 حذف شده
  1. 16 7
      apps/proxy/src/proxy/proxy_test.py
  2. 11 7
      apps/proxy/src/proxy/views.py

+ 16 - 7
apps/proxy/src/proxy/proxy_test.py

@@ -17,10 +17,14 @@
 #
 # Tests for proxy app.
 
+from __future__ import print_function
+from future import standard_library
+standard_library.install_aliases()
+from builtins import str
 import threading
 import logging
-import BaseHTTPServer
-import StringIO
+import http.server
+import sys
 
 from nose.tools import assert_true, assert_false
 from django.test.client import Client
@@ -29,8 +33,13 @@ from desktop.lib.django_test_util import make_logged_in_client
 from proxy.views import _rewrite_links
 import proxy.conf
 
+if sys.version_info[0] > 2:
+  from io import StringIO as string_io
+else:
+  from StringIO import StringIO as string_io
 
-class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
+
+class Handler(http.server.BaseHTTPRequestHandler):
   """
   To avoid mocking out urllib, we setup a web server
   that does very little, and test proxying against it.
@@ -66,7 +75,7 @@ def run_test_server():
   Returns the server, and a method to close it out.
   """
   # We need to proxy a server, so we go ahead and create one.
-  httpd = BaseHTTPServer.HTTPServer(("127.0.0.1", 0), Handler)
+  httpd = http.server.HTTPServer(("127.0.0.1", 0), Handler)
   # Spawn a thread that serves exactly one request.
   thread = threading.Thread(target=httpd.handle_request)
   thread.daemon = True
@@ -74,7 +83,7 @@ def run_test_server():
 
   def finish():
     # Make sure the server thread is done.
-    print "Closing thread " + str(thread)
+    print("Closing thread " + str(thread))
     thread.join(10.0) # Wait at most 10 seconds
     assert_false(thread.isAlive())
 
@@ -148,12 +157,12 @@ def test_blacklist():
       fin()
 
 
-class UrlLibFileWrapper(StringIO.StringIO):
+class UrlLibFileWrapper(string_io):
   """
   urllib2.urlopen returns a file-like object; we fake it here.
   """
   def __init__(self, buf, url):
-    StringIO.StringIO.__init__(self, buf)
+    string_io.__init__(self, buf)
     self.url = url
 
   def geturl(self):

+ 11 - 7
apps/proxy/src/proxy/views.py

@@ -23,10 +23,13 @@
 # to create links (within the application) to trusted
 # URLs, by appending an HMAC to the parameters.
 
+from future import standard_library
+standard_library.install_aliases()
+from builtins import str
 import logging
 import re
-from urllib2 import urlopen
-from urlparse import urlparse, urlunparse
+from urllib.request import Request, urlopen
+from urllib.parse import urlencode, urlparse, urlunparse
 
 from django.core import urlresolvers
 from django.http import HttpResponse
@@ -89,17 +92,18 @@ def proxy(request, host, port, path):
 
   # The tuple here is: (scheme, netloc, path, params, query, fragment).
   # We don't support params or fragment.
-  url = urlunparse(("http", "%s:%d" % (host,port), 
+  url = urlunparse((u'http', "%s:%d" % (host,port),
                     path, 
                     None, 
-                    request.META.get("QUERY_STRING"), 
+                    str(request.META.get("QUERY_STRING")),
                     None))
   LOGGER.info("Retrieving %s." % url)
   if request.method == 'POST':
-    post_data = request.POST.urlencode()
+    post_data = urlencode(dict(zip(request.POST.keys(), request.POST.values()))).encode('ascii')
   else:
     post_data = None
-  data = urlopen(url, data=post_data)
+  req = Request(url, data=post_data)
+  data = urlopen(req)
   content_type = data.headers.get("content-type", "text/plain")
   if not re.match(r'^text/html\s*(?:;.*)?$', content_type):
     resp_text = data.read(1024*1024) # read 1MB
@@ -127,7 +131,7 @@ def _rewrite_url(url):
   try:
     # We may hit invalid urls. Return None to strip out the link entirely.
     out = _reverse(host, port, path)
-  except urlresolvers.NoReverseMatch, ex:
+  except urlresolvers.NoReverseMatch as ex:
     LOGGER.error("Encountered malformed URL '%s' when rewriting proxied page." % (url,))
     return None