Code

Import md5 from hashlib/md5, dep. on Py version
authorSven Velt <sven@velt.de>
Wed, 16 Dec 2009 16:35:45 +0000 (17:35 +0100)
committerSven Velt <sven@velt.de>
Wed, 16 Dec 2009 16:35:45 +0000 (17:35 +0100)
2.4: Only "md5" available
2.5: "md5" and "hashlib" work
2.6: Only "hashlib"

So try to "from hashlib import md5" and if it doesn't work just do a
"from md5 import md5" and just use "md5(___).hexdigest()" in the code.

nagixsc_conf2http.py
nagixsc_http2nagios.py

index d5200e09e34102b91729145eeebb8d4988827a23..995fa453f85e288958390823fbda4a2be7883c41 100755 (executable)
@@ -2,11 +2,15 @@
 
 import BaseHTTPServer
 import base64
-import md5
 import os
 import re
 import subprocess
 
+try:
+       from hashlib import md5
+except ImportError:
+       from md5 import md5
+
 config = {     'ip':                   '',
                        'port':                 15666,
                }
@@ -19,7 +23,7 @@ C2X='./nagixsc_conf2xml.py'
 
 class Conf2HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
 
-       def http_error(code, output):
+       def http_error(self, code, output):
                self.send_response(code)
                self.send_header('Content-Type', 'text/plain')
                self.end_headers()
@@ -35,7 +39,7 @@ class Conf2HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
                # Check Basic Auth
                try:
                        authdata = base64.b64decode(self.headers['Authorization'].split(' ')[1]).split(':')
-                       if not users[authdata[0]] == md5.md5(authdata[1]).hexdigest():
+                       if not users[authdata[0]] == md5(authdata[1]).hexdigest():
                                raise Exception
                except:
                        self.send_response(401)
@@ -62,10 +66,10 @@ class Conf2HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
                        configfile =''
 
                if re.search('\.\.', configfile):
-                       http_error(500, 'Found ".." in config file name')
+                       self.http_error(500, 'Found ".." in config file name')
                        return
                if configfile and not re.search('^[a-zA-Z0-9-_\.]+$', configfile):
-                       http_error(500, 'Config file name contains invalid characters')
+                       self.http_error(500, 'Config file name contains invalid characters')
                        return
 
                if configfile:
@@ -82,7 +86,7 @@ class Conf2HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
                        output  = cmd.communicate()[0].rstrip()
                        retcode = cmd.returncode
                except OSError:
-                       http_error(500, 'Could not execute "%s"' % cmdline)
+                       self.http_error(500, 'Could not execute "%s"' % cmdline)
                        return
 
                if retcode == 0:
@@ -91,7 +95,7 @@ class Conf2HTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
                        self.end_headers()
                        self.wfile.write(output)
                else:
-                       http_error(500, output)
+                       self.http_error(500, output)
 
                return
 
index 034213885c9e48bcbab4436aa7552b65bf0e7e8c..e0a639b3218cd6595624b13cbe46fb358ddf4052 100755 (executable)
@@ -3,11 +3,15 @@
 import BaseHTTPServer
 import base64
 import cgi
-import md5
 import os
 import re
 import subprocess
 
+try:
+       from hashlib import md5
+except ImportError:
+       from md5 import md5
+
 config = {     'ip':                   '',
                        'port':                 15667,
                }
@@ -20,7 +24,7 @@ X2N='./nagixsc_xml2nagios.py -O passive -vvv -f -'
 
 class HTTP2NagiosHandler(BaseHTTPServer.BaseHTTPRequestHandler):
 
-       def http_error(code, output):
+       def http_error(self, code, output):
                self.send_response(code)
                self.send_header('Content-Type', 'text/plain')
                self.end_headers()
@@ -48,7 +52,7 @@ class HTTP2NagiosHandler(BaseHTTPServer.BaseHTTPRequestHandler):
                # Check Basic Auth
                try:
                        authdata = base64.b64decode(self.headers['Authorization'].split(' ')[1]).split(':')
-                       if not users[authdata[0]] == md5.md5(authdata[1]).hexdigest():
+                       if not users[authdata[0]] == md5(authdata[1]).hexdigest():
                                raise Exception
                except:
                        self.send_response(401)