Make code compatibile with python 2 and 3
Add chcecks for version for importing and decoding bytestring. Inspired by PR #66
This commit is contained in:
parent
08fb44b66d
commit
9f01a465d8
|
|
@ -4,8 +4,15 @@
|
||||||
|
|
||||||
# Copyright (C) 2014-2015 Nginx, Inc.
|
# Copyright (C) 2014-2015 Nginx, Inc.
|
||||||
|
|
||||||
import sys, os, signal, base64, ldap, Cookie, argparse
|
import sys, os, signal, base64, ldap, argparse
|
||||||
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
|
if sys.version_info.major == 2:
|
||||||
|
from Cookie import BaseCookie
|
||||||
|
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
|
||||||
|
elif sys.version_info.major == 3:
|
||||||
|
from http.cookies import BaseCookie
|
||||||
|
from http.server import HTTPServer, BaseHTTPRequestHandler
|
||||||
|
|
||||||
|
if not hasattr(__builtins__, "basestring"): basestring = (str, bytes)
|
||||||
|
|
||||||
#Listen = ('localhost', 8888)
|
#Listen = ('localhost', 8888)
|
||||||
#Listen = "/tmp/auth.sock" # Also uncomment lines in 'Requests are
|
#Listen = "/tmp/auth.sock" # Also uncomment lines in 'Requests are
|
||||||
|
|
@ -16,7 +23,11 @@ from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
# Requests are processed in separate thread
|
# Requests are processed in separate thread
|
||||||
import threading
|
import threading
|
||||||
from SocketServer import ThreadingMixIn
|
if sys.version_info.major == 2:
|
||||||
|
from SocketServer import ThreadingMixIn
|
||||||
|
elif sys.version_info.major == 3:
|
||||||
|
from socketserver import ThreadingMixIn
|
||||||
|
|
||||||
class AuthHTTPServer(ThreadingMixIn, HTTPServer):
|
class AuthHTTPServer(ThreadingMixIn, HTTPServer):
|
||||||
pass
|
pass
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
|
@ -71,6 +82,7 @@ class AuthHandler(BaseHTTPRequestHandler):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
auth_decoded = base64.b64decode(auth_header[6:])
|
auth_decoded = base64.b64decode(auth_header[6:])
|
||||||
|
if sys.version_info.major == 3: auth_decoded = auth_decoded.decode("utf-8")
|
||||||
user, passwd = auth_decoded.split(':', 1)
|
user, passwd = auth_decoded.split(':', 1)
|
||||||
|
|
||||||
except:
|
except:
|
||||||
|
|
@ -86,7 +98,7 @@ class AuthHandler(BaseHTTPRequestHandler):
|
||||||
def get_cookie(self, name):
|
def get_cookie(self, name):
|
||||||
cookies = self.headers.get('Cookie')
|
cookies = self.headers.get('Cookie')
|
||||||
if cookies:
|
if cookies:
|
||||||
authcookie = Cookie.BaseCookie(cookies).get(name)
|
authcookie = BaseCookie(cookies).get(name)
|
||||||
if authcookie:
|
if authcookie:
|
||||||
return authcookie.value
|
return authcookie.value
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue