diff options
Diffstat (limited to 'netlib')
-rw-r--r-- | netlib/certutils.py | 11 | ||||
-rw-r--r-- | netlib/tcp.py | 4 | ||||
-rw-r--r-- | netlib/wsgi.py | 31 |
3 files changed, 25 insertions, 21 deletions
diff --git a/netlib/certutils.py b/netlib/certutils.py index 9193b757..df793537 100644 --- a/netlib/certutils.py +++ b/netlib/certutils.py @@ -5,6 +5,8 @@ import time import datetime import itertools import ipaddress + +import sys from pyasn1.type import univ, constraint, char, namedtype, tag from pyasn1.codec.der.decoder import decode from pyasn1.error import PyAsn1Error @@ -184,7 +186,7 @@ class CertStore(object): with open(path, "wb") as f: f.write(DEFAULT_DHPARAM) - bio = OpenSSL.SSL._lib.BIO_new_file(path, b"r") + bio = OpenSSL.SSL._lib.BIO_new_file(path.encode(sys.getfilesystemencoding()), b"r") if bio != OpenSSL.SSL._ffi.NULL: bio = OpenSSL.SSL._ffi.gc(bio, OpenSSL.SSL._lib.BIO_free) dh = OpenSSL.SSL._lib.PEM_read_bio_DHparams( @@ -318,10 +320,9 @@ class CertStore(object): potential_keys.append((commonname, tuple(sans))) name = next( - itertools.ifilter( - lambda key: key in self.certs, - potential_keys), - None) + filter(lambda key: key in self.certs, potential_keys), + None + ) if name: entry = self.certs[name] else: diff --git a/netlib/tcp.py b/netlib/tcp.py index 707e11e0..6dcc8c72 100644 --- a/netlib/tcp.py +++ b/netlib/tcp.py @@ -76,7 +76,7 @@ class SSLKeyLogger(object): d = os.path.dirname(self.filename) if not os.path.isdir(d): os.makedirs(d) - self.f = open(self.filename, "ab") + self.f = open(self.filename, "a") self.f.write("\r\n") client_random = connection.client_random().encode("hex") masterkey = connection.master_key().encode("hex") @@ -184,7 +184,7 @@ class Reader(_FileLike): """ If length is -1, we read until connection closes. """ - result = '' + result = b'' start = time.time() while length == -1 or length > 0: if length == -1 or length > self.BLOCKSIZE: diff --git a/netlib/wsgi.py b/netlib/wsgi.py index 8a98884a..fba9f388 100644 --- a/netlib/wsgi.py +++ b/netlib/wsgi.py @@ -1,8 +1,11 @@ from __future__ import (absolute_import, print_function, division) -import cStringIO +from io import BytesIO import urllib import time import traceback + +import six + from . import http, tcp @@ -58,7 +61,7 @@ class WSGIAdaptor(object): environ = { 'wsgi.version': (1, 0), 'wsgi.url_scheme': flow.request.scheme, - 'wsgi.input': cStringIO.StringIO(flow.request.body or ""), + 'wsgi.input': BytesIO(flow.request.body or b""), 'wsgi.errors': errsoc, 'wsgi.multithread': True, 'wsgi.multiprocess': False, @@ -91,17 +94,17 @@ class WSGIAdaptor(object): Make a best-effort attempt to write an error page. If headers are already sent, we just bung the error into the page. """ - c = """ + c = b""" <html> <h1>Internal Server Error</h1> <pre>%s"</pre> </html> - """ % s + """.strip() % s if not headers_sent: - soc.write("HTTP/1.1 500 Internal Server Error\r\n") - soc.write("Content-Type: text/html\r\n") - soc.write("Content-Length: %s\r\n" % len(c)) - soc.write("\r\n") + soc.write(b"HTTP/1.1 500 Internal Server Error\r\n") + soc.write(b"Content-Type: text/html\r\n") + soc.write(b"Content-Length: %s\r\n" % len(c)) + soc.write(b"\r\n") soc.write(c) def serve(self, request, soc, **env): @@ -114,14 +117,14 @@ class WSGIAdaptor(object): def write(data): if not state["headers_sent"]: - soc.write("HTTP/1.1 %s\r\n" % state["status"]) + soc.write(b"HTTP/1.1 %s\r\n" % state["status"]) headers = state["headers"] if 'server' not in headers: headers["Server"] = self.sversion if 'date' not in headers: headers["Date"] = date_time_string() - soc.write(str(headers)) - soc.write("\r\n") + soc.write(bytes(headers)) + soc.write(b"\r\n") state["headers_sent"] = True if data: soc.write(data) @@ -131,7 +134,7 @@ class WSGIAdaptor(object): if exc_info: try: if state["headers_sent"]: - raise exc_info[0], exc_info[1], exc_info[2] + six.reraise(*exc_info) finally: exc_info = None elif state["status"]: @@ -140,7 +143,7 @@ class WSGIAdaptor(object): state["headers"] = http.Headers(headers) return write - errs = cStringIO.StringIO() + errs = BytesIO() try: dataiter = self.app( self.make_environ(request, errs, **env), start_response @@ -148,7 +151,7 @@ class WSGIAdaptor(object): for i in dataiter: write(i) if not state["headers_sent"]: - write("") + write(b"") except Exception as e: try: s = traceback.format_exc() |