[Spambayes-checkins] SF.net SVN: spambayes:[3194] trunk/spambayes/spambayes/Dibbler.py

montanaro at users.sourceforge.net montanaro at users.sourceforge.net
Tue Nov 25 02:59:43 CET 2008


Revision: 3194
          http://spambayes.svn.sourceforge.net/spambayes/?rev=3194&view=rev
Author:   montanaro
Date:     2008-11-25 01:59:43 +0000 (Tue, 25 Nov 2008)

Log Message:
-----------
Use hashlib.md5 if available.  pylint nits.  errors go to stderr.

Modified Paths:
--------------
    trunk/spambayes/spambayes/Dibbler.py

Modified: trunk/spambayes/spambayes/Dibbler.py
===================================================================
--- trunk/spambayes/spambayes/Dibbler.py	2008-11-25 01:57:51 UTC (rev 3193)
+++ trunk/spambayes/spambayes/Dibbler.py	2008-11-25 01:59:43 UTC (rev 3194)
@@ -169,10 +169,14 @@
 except ImportError:
     import StringIO
 
-import sys, re, time, traceback, md5, base64
+import sys, re, time, traceback, base64
 import socket, asyncore, asynchat, cgi, urlparse, webbrowser
 
 try:
+    from hashlib import md5
+except ImportError:
+    from md5 import new as md5
+try:
     True, False
 except NameError:
     # Maintain compatibility with Python 2.2
@@ -283,7 +287,11 @@
         self.set_reuse_addr()
         if type(port) != type(()):
             port = ('', port)
-        self.bind(port)
+        try:
+            self.bind(port)
+        except socket.error:
+            print >> sys.stderr, "port", port, "in use"
+            raise
         self.listen(5)
 
     def handle_accept(self):
@@ -456,7 +464,7 @@
                 elif authenticationMode == HTTPServer.DIGEST_AUTHENTICATION:
                     authResult = self._digestAuthentication(login, method)
                 else:
-                    print >>sys.stdout, "Unknown mode: %s" % authenticationMode
+                    print >> sys.stderr, "Unknown mode: %s" % authenticationMode
 
             if not authResult:
                 self.writeUnauthorizedAccess(serverAuthMode)
@@ -625,7 +633,8 @@
         """Check if the specified nonce is still valid. A nonce is invalid
         when its time converted value is lower than current time."""
         padAmount = len(nonce) % 4
-        if padAmount > 0: padAmount = 4 - padAmount
+        if padAmount > 0:
+            padAmount = 4 - padAmount
         nonce += '=' * (len(nonce) + padAmount)
 
         decoded = base64.decodestring(nonce)
@@ -650,9 +659,9 @@
 
         # The following computations are based upon RFC 2617.
         A1  = "%s:%s:%s" % (userName, self._server.getRealm(), password)
-        HA1 = md5.new(A1).hexdigest()
+        HA1 = md5(A1).hexdigest()
         A2  = "%s:%s" % (method, stripQuotes(options["uri"]))
-        HA2 = md5.new(A2).hexdigest()
+        HA2 = md5(A2).hexdigest()
 
         unhashedDigest = ""
         if options.has_key("qop"):
@@ -669,7 +678,7 @@
                              stripQuotes(options["qop"]), HA2)
         else:
             unhashedDigest = "%s:%s:%s" % (HA1, nonce, HA2)
-        hashedDigest = md5.new(unhashedDigest).hexdigest()
+        hashedDigest = md5(unhashedDigest).hexdigest()
 
         return (stripQuotes(options["response"]) == hashedDigest and
                 self._isValidNonce(nonce))
@@ -735,8 +744,8 @@
 
 def runTestServer(readyEvent=None):
     """Runs the calendar server example, with an added `/shutdown` URL."""
-    import Dibbler, calendar
-    class Calendar(Dibbler.HTTPPlugin):
+    import calendar
+    class Calendar(HTTPPlugin):
         _form = '''<html><body><h3>Calendar Server</h3>
                    <form action='/'>
                    Year: <input type='text' name='year' size='4'>
@@ -757,12 +766,12 @@
             self.close()
             sys.exit()
 
-    httpServer = Dibbler.HTTPServer(8888)
+    httpServer = HTTPServer(8888)
     httpServer.register(Calendar())
     if readyEvent:
         # Tell the self-test code that the test server is up and running.
         readyEvent.set()
-    Dibbler.run(launchBrowser=True)
+    run(launchBrowser=True)
 
 def test():
     """Run a self-test."""


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.


More information about the Spambayes-checkins mailing list