Commits

Martin von Löwis  committed 8cd3dca

Use IPv6/v4 wildcard if requested.

  • Participants
  • Parent commits a01557d

Comments (0)

Files changed (1)

File openid2rp/testapp.py

 #!/usr/bin/env python
 ################ Test Server #################################
-import BaseHTTPServer, cgi
+import BaseHTTPServer, cgi, socket
 from openid2rp import *
 
 # supported providers
     def not_found(self):
         self.send_response(404)
         self.end_headers()
+
+if hasattr(socket, 'IPV6_V6ONLY'):
+    class HTTPServer(BaseHTTPServer.HTTPServer):
+        def __init__(self, addr, handler):
+            if not addr[0]:
+                # use V6 here, set to wildcard below
+                self.address_family = socket.AF_INET6
+            BaseHTTPServer.HTTPServer.__init__(self, addr, handler)
+        def server_bind(self):
+            if self.address_family == socket.AF_INET6:
+                self.socket.setsockopt(socket.IPPROTO_IPV6,
+                                       socket.IPV6_V6ONLY,
+                                       False)
+            BaseHTTPServer.HTTPServer.server_bind(self)
+else:
+    HTTPServer = BaseHTTPServer.HTTPServer
+
         
 # OpenID providers often attempt relying-party discovery
 # This requires the test server to use a globally valid URL
         base_url = "http://" + socket.getfqdn() + ":8000/"
     print "Listening on", base_url
     Handler.base_url = base_url
-    #BaseHTTPServer.HTTPServer.address_family = socket.AF_INET6
-    httpd = BaseHTTPServer.HTTPServer(('', 8000), Handler)
+    httpd = HTTPServer(('', 8000), Handler)
     httpd.serve_forever()
 
 if __name__ == '__main__':