Skip to content
Browse files

Merge pull request #1 from pigmej/master

Fixes the PySCGI server class binding method. It used to use the first interface address available for the binding, but it did not check whether or not it succeed. Moreover, in case it did not it was not retrying with the next address (in case there were more than one).
  • Loading branch information...
2 parents e555e87 + d0520c2 commit 1e0c777ac78715c794caf17d84a06a733bfbac82 @alobbs alobbs committed Oct 13, 2011
Showing with 32 additions and 1 deletion.
  1. +32 −1 pyscgi/pyscgi.py
View
33 pyscgi/pyscgi.py
@@ -185,7 +185,38 @@ def process_request (self, request, client_address):
class ThreadingUnixStreamServer_Custom (ThreadingMixIn_Custom, SocketServer.UnixStreamServer): pass
-class ThreadingTCPServer_Custom (ThreadingMixIn_Custom, SocketServer.TCPServer): pass
+class ThreadingTCPServer_Custom (ThreadingMixIn_Custom, SocketServer.TCPServer):
+ def server_bind(self):
+ HOST, PORT = self.server_address
+ s = None
+ # loop over ifaces for the HOST and PORT pair
+ for res in socket.getaddrinfo(HOST,
+ PORT,
+ socket.AF_UNSPEC,
+ socket.SOCK_STREAM,
+ 0,
+ socket.AI_PASSIVE):
+ af, socktype, protocol, canonicalname, sa = res
+ try:
+ s = socket.socket(af, socktype, protocol)
+ except socket.error:
+ s = None
+ continue
+ try:
+ if self.allow_reuse_address:
+ s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ s.bind(sa)
+ except socket.error:
+ s.close()
+ s = None
+ continue
+ break
+ # if none successfully bind report error
+ if s is None:
+ raise socket.error, "Can't open socket"
+ self.socket = s
+ # fix the server_address
+ self.server_address = self.socket.getsockname()
# TCP port

0 comments on commit 1e0c777

Please sign in to comment.
Something went wrong with that request. Please try again.