Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1665

Show
Ignore:
Timestamp:
06/16/07 16:25:01
Author:
fumanchu
Message:

Fix for #643 (socket_host is confusing). Empty string and None are now illegal values for socket_host; use '0.0.0.0' instead for INADDR_ANY.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/_cpserver.py

    r1660 r1665  
    2929    control them all through this object: 
    3030     
    31         s1 = MyWSGIServer(host='', port=80) 
     31        s1 = MyWSGIServer(host='0.0.0.0', port=80) 
    3232        s2 = another.HTTPServer(host='localhost', SSL=True) 
    33         cherrypy.server.httpservers = {s1: ('', 80), s2: ('localhost', 443)} 
     33        cherrypy.server.httpservers = {s1: ('0.0.0.0', 80), 
     34                                       s2: ('localhost', 443)} 
    3435        # Note we do not use quickstart when we define our own httpservers 
    3536        cherrypy.server.start() 
     
    4142     
    4243    socket_port = 8080 
    43     socket_host = '' 
     44     
     45    _socket_host = 'localhost' 
     46    def _get_socket_host(self): 
     47        return self._socket_host 
     48    def _set_socket_host(self, value): 
     49        if not value: 
     50            raise ValueError("Host values of '' or None are not allowed. " 
     51                             "Use '0.0.0.0' instead to listen on all active " 
     52                             "interfaces (INADDR_ANY).") 
     53        self._socket_host = value 
     54    socket_host = property(_get_socket_host, _set_socket_host, 
     55        doc="""The hostname or IP address on which to listen for connections. 
     56         
     57        Valid host values include any IPv4 or IPv6 address, any valid 
     58        hostname, 'localhost' as a synonym for '127.0.0.1', and '0.0.0.0' 
     59        as a special entry meaning "all active interfaces" (INADDR_ANY). 
     60        The empty string or None are not allowed.""") 
     61     
    4462    socket_file = '' 
    4563    socket_queue_size = 5 
     
    106124            wait_for_free_port(*bind_addr) 
    107125            host, port = bind_addr 
    108             if not host: 
    109                 host = '0.0.0.0' 
    110126            on_what = "%s://%s:%s/" % (scheme, host, port) 
    111127        else: 
     
    157173            if isinstance(bind_addr, tuple): 
    158174                host, port = bind_addr 
    159                 if not host or host == '0.0.0.0': 
    160                     host = socket.gethostname() 
     175                if host == '0.0.0.0': 
     176                    # 0.0.0.0 is INADDR_ANY, which should answer on localhost. 
     177                    host = 'localhost' 
    161178                wait_for_occupied_port(host, port) 
    162179     
     
    186203         
    187204        host = self.socket_host 
    188         if not host or host == '0.0.0.0': 
    189             # The empty string signifies INADDR_ANY. Look up the host name, 
     205        if host == '0.0.0.0': 
     206            # 0.0.0.0 is INADDR_ANY. Look up the host name, 
    190207            # which should be the safest thing to spit out in a URL. 
    191208            host = socket.gethostname() 
     
    208225    """Raise an error if the given port is not free on the given host.""" 
    209226    if not host: 
    210         # The empty string signifies INADDR_ANY, 
    211         # which should respond on localhost. 
     227        raise ValueError("Host values of '' or None are not allowed.") 
     228    if host == '0.0.0.0': 
     229        # 0.0.0.0 is INADDR_ANY, which should answer on localhost. 
    212230        host = 'localhost' 
    213231    port = int(port) 
     
    237255    """Wait for the specified port to become free (drop requests).""" 
    238256    if not host: 
    239         # The empty string signifies INADDR_ANY, 
    240         # which should respond on localhost. 
     257        raise ValueError("Host values of '' or None are not allowed.") 
     258    if host == '0.0.0.0': 
     259        # 0.0.0.0 is INADDR_ANY, which should answer on localhost. 
    241260        host = 'localhost' 
    242261     
     
    257276    """Wait for the specified port to become active (receive requests).""" 
    258277    if not host: 
    259         # The empty string signifies INADDR_ANY, 
    260         # which should respond on localhost. 
     278        raise ValueError("Host values of '' or None are not allowed.") 
     279    if host == '0.0.0.0': 
     280        # 0.0.0.0 is INADDR_ANY, which should answer on localhost. 
    261281        host = 'localhost' 
    262282     
  • trunk/cherrypy/test/test_core.py

    r1658 r1665  
    484484         
    485485        host = self.HOST 
    486         if not host: 
    487             # The empty string signifies INADDR_ANY, 
    488             # which should respond on localhost. 
     486        if host == '0.0.0.0': 
     487            # INADDR_ANY, which should respond on localhost. 
    489488            host = "127.0.0.1" 
    490489        intro = '%s - - [' % host 
  • trunk/cherrypy/test/test_proxy.py

    r1657 r1665  
    9292                port = ":%s" % self.PORT 
    9393            host = self.HOST 
    94             if host == '': 
     94            if host == '0.0.0.0': 
    9595                import socket 
    9696                host = socket.gethostname() 
  • trunk/cherrypy/test/test_xmlrpc.py

    r1663 r1665  
    8989         
    9090        self.verbose = verbose 
     91         
     92        # Here's where we differ from the superclass. It says: 
     93        # try: 
     94        #     sock = h._conn.sock 
     95        # except AttributeError: 
     96        #     sock = None 
     97        # return self._parse_response(h.getfile(), sock) 
     98         
    9199        return self.parse_response(h.getfile()) 
    92100 
     
    104112            pass 
    105113         
     114        host = self.HOST 
     115        if host == '0.0.0.0': 
     116            # INADDR_ANY, which should respond on localhost. 
     117            host = "127.0.0.1" 
     118         
    106119        if scheme == "https": 
    107             url = 'https://%s:%s/xmlrpc/' % (self.HOST, self.PORT) 
     120            url = 'https://%s:%s/xmlrpc/' % (host, self.PORT) 
    108121            proxy = xmlrpclib.ServerProxy(url, transport=HTTPSTransport()) 
    109122        else: 
    110             url = 'http://%s:%s/xmlrpc/' % (self.HOST, self.PORT) 
     123            url = 'http://%s:%s/xmlrpc/' % (host, self.PORT) 
    111124            proxy = xmlrpclib.ServerProxy(url) 
    112125         
  • trunk/cherrypy/test/webtest.py

    r1532 r1665  
    165165        if on: 
    166166            host = self.HOST 
    167             if not host: 
    168                 # The empty string signifies INADDR_ANY, 
    169                 # which should respond on localhost. 
     167            if host == '0.0.0.0': 
     168                # INADDR_ANY, which should respond on localhost. 
    170169                host = "127.0.0.1" 
    171170            self.HTTP_CONN = cls(host, self.PORT) 
     
    187186         
    188187        self.url = url 
    189         host = self.HOST 
    190         if not host: 
    191             # The empty string signifies INADDR_ANY, 
    192             # which should respond on localhost. 
    193             host = "127.0.0.1" 
    194         result = openURL(url, headers, method, body, host, self.PORT, 
     188        result = openURL(url, headers, method, body, self.HOST, self.PORT, 
    195189                         self.HTTP_CONN, protocol or self.PROTOCOL) 
    196190        self.status, self.headers, self.body = result 
     
    476470                conn = http_conn 
    477471            else: 
     472                if host == '0.0.0.0': 
     473                    # INADDR_ANY, which should respond on localhost. 
     474                    host = "127.0.0.1" 
    478475                conn = http_conn(host, port) 
    479476             
  • trunk/cherrypy/wsgiserver/__init__.py

    r1660 r1665  
    730730    """An HTTP server for WSGI. 
    731731     
    732     bind_addr: a (host, port) tuple if TCP sockets are desired; 
    733         for UNIX sockets, supply the filename as a string. 
     732    bind_addr: The interface on which to listen for connections. 
     733        For TCP sockets, a (host, port) tuple. Valid host values include: 
     734        any IPv4 or IPv6 address, any valid hostname, 'localhost' as a 
     735        synonym for '127.0.0.1', and '0.0.0.0' as a special entry meaning 
     736        "all active interfaces" (INADDR_ANY). The empty string or None 
     737        are not allowed. 
     738        For UNIX sockets, supply the filename as a string. 
    734739    wsgi_app: the WSGI 'application callable'; multiple WSGI applications 
    735740        may be passed as (script_name, callable) pairs. 
     
    761766     
    762767    protocol = "HTTP/1.1" 
     768    _bind_addr = "localhost" 
    763769    version = "CherryPy/3.1alpha" 
    764770    ready = False 
     
    796802        self.timeout = timeout 
    797803        self.shutdown_timeout = shutdown_timeout 
     804     
     805    def _get_bind_addr(self): 
     806        return self._bind_addr 
     807    def _set_bind_addr(self, value): 
     808        if isinstance(value, tuple) and value[0] in ('', None): 
     809            # Despite the socket module docs, using '' does not 
     810            # allow AI_PASSIVE to work. Passing None instead 
     811            # returns '0.0.0.0' like we want. In other words: 
     812            #     host    AI_PASSIVE     result 
     813            #      ''         Y         192.168.x.y 
     814            #      ''         N         192.168.x.y 
     815            #     None        Y         0.0.0.0 
     816            #     None        N         127.0.0.1 
     817            # But since you can get the same effect with an explicit 
     818            # '0.0.0.0', we deny both the empty string and None as values. 
     819            raise ValueError("Host values of '' or None are not allowed. " 
     820                             "Use '0.0.0.0' instead to listen on all active " 
     821                             "interfaces (INADDR_ANY).") 
     822        self._bind_addr = value 
     823    bind_addr = property(_get_bind_addr, _set_bind_addr, 
     824        doc="""The interface on which to listen for connections. 
     825         
     826        For TCP sockets, a (host, port) tuple. Valid host values include: 
     827        any IPv4 or IPv6 address, any valid hostname, 'localhost' as a 
     828        synonym for '127.0.0.1', and '0.0.0.0' as a special entry meaning 
     829        "all active interfaces" (INADDR_ANY). The empty string or None 
     830        are not allowed. 
     831         
     832        For UNIX sockets, supply the filename as a string.""") 
    798833     
    799834    def start(self): 
     
    822857            # Get the correct address family for our host (allows IPv6 addresses) 
    823858            host, port = self.bind_addr 
    824             flags = 0 
    825             if host == '': 
    826                 # Despite the socket module docs, using '' does not 
    827                 # allow AI_PASSIVE to work. Passing None instead 
    828                 # returns '0.0.0.0' like we want. In other words: 
    829                 #     host    AI_PASSIVE     result 
    830                 #      ''         Y         192.168.x.y 
    831                 #      ''         N         192.168.x.y 
    832                 #     None        Y         0.0.0.0 
    833                 #     None        N         127.0.0.1 
    834                 host = None 
    835                 flags = socket.AI_PASSIVE 
    836859            try: 
    837860                info = socket.getaddrinfo(host, port, socket.AF_UNSPEC, 
    838                                           socket.SOCK_STREAM, 0, flags
     861                                          socket.SOCK_STREAM, 0, socket.AI_PASSIVE
    839862            except socket.gaierror: 
    840863                # Probably a DNS issue. Assume IPv4. 
     
    949972                    # here, because we want an actual IP to touch. 
    950973                    # localhost won't work if we've bound to a public IP, 
    951                     # but it would if we bound to INADDR_ANY via host = ''
     974                    # but it will if we bound to '0.0.0.0' (INADDR_ANY)
    952975                    for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, 
    953976                                                  socket.SOCK_STREAM): 

Hosted by WebFaction

Log in as guest/cpguest to create tickets