Changeset 112:3b07cc2b0c76

Show
Ignore:
Timestamp:
02/20/10 08:33:12 (5 months ago)
Author:
Allan Saddi <allan@saddi.com>
branch:
default
Message:

avoid socket.fromfd AttributeError? on win32 if cgi is forced, give helpful exception for fcgi
(by Thomas Waldmann, via moinmoin, changesets 49f8dd576950, 661057dc4d09)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • flup/server/fcgi_base.py

    r111 r112  
    10011001 
    10021002    def _setupSocket(self): 
    1003         if self._bindAddress is None: # Run as a normal FastCGI? 
    1004             isFCGI = True 
    1005  
    1006             sock = socket.fromfd(FCGI_LISTENSOCK_FILENO, socket.AF_INET, 
    1007                                  socket.SOCK_STREAM) 
    1008             try: 
    1009                 sock.getpeername() 
    1010             except socket.error, e: 
    1011                 if e[0] == errno.ENOTSOCK: 
    1012                     # Not a socket, assume CGI context. 
    1013                     isFCGI = False 
    1014                 elif e[0] != errno.ENOTCONN: 
    1015                     raise 
     1003        if self._bindAddress is None: 
     1004            # Run as a normal FastCGI? 
    10161005 
    10171006            # FastCGI/CGI discrimination is broken on Mac OS X. 
     
    10201009            # this with Apache's mod_env [not loaded by default in OS X 
    10211010            # client, ha ha] and the SetEnv directive.) 
    1022             if not isFCGI or self.forceCGI or \ 
    1023                os.environ.get('FCGI_FORCE_CGI', 'N').upper().startswith('Y'): 
     1011            forceCGI = self.forceCGI or \ 
     1012               os.environ.get('FCGI_FORCE_CGI', 'N').upper().startswith('Y') 
     1013 
     1014            if forceCGI: 
     1015                isFCGI = False 
     1016            else: 
     1017                if not hasattr(socket, 'fromfd'): 
     1018                    # can happen on win32, no socket.fromfd there! 
     1019                    raise ValueError( 
     1020                        'If you want FCGI, please create an external FCGI server ' 
     1021                        'by providing a valid bindAddress. ' 
     1022                        'If you want CGI, please force CGI operation. Use ' 
     1023                        'FCGI_FORCE_CGI=Y environment or forceCGI parameter.') 
     1024                sock = socket.fromfd(FCGI_LISTENSOCK_FILENO, socket.AF_INET, 
     1025                                     socket.SOCK_STREAM) 
     1026                isFCGI = True 
     1027                try: 
     1028                    sock.getpeername() 
     1029                except socket.error, e: 
     1030                    if e[0] == errno.ENOTSOCK: 
     1031                        # Not a socket, assume CGI context. 
     1032                        isFCGI = False 
     1033                    elif e[0] != errno.ENOTCONN: 
     1034                        raise 
     1035 
     1036            if not isFCGI: 
    10241037                req = self.cgirequest_class(self) 
    10251038                req.run()