From f6f4269adfe6910bc4eeb6bbe63036a32da30147 Mon Sep 17 00:00:00 2001 From: Sven Velt Date: Wed, 16 Dec 2009 17:35:45 +0100 Subject: [PATCH] Import md5 from hashlib/md5, dep. on Py version 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 | 18 +++++++++++------- nagixsc_http2nagios.py | 10 +++++++--- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/nagixsc_conf2http.py b/nagixsc_conf2http.py index d5200e0..995fa45 100755 --- a/nagixsc_conf2http.py +++ b/nagixsc_conf2http.py @@ -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 diff --git a/nagixsc_http2nagios.py b/nagixsc_http2nagios.py index 0342138..e0a639b 100755 --- a/nagixsc_http2nagios.py +++ b/nagixsc_http2nagios.py @@ -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) -- 2.30.2