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

root/branches/598-sendall/cherrypy/_cpserver.py

Revision 1837 (checked in by fumanchu, 11 months ago)

Fix for #756 (Deprecate server.quickstart):

  • server.quickstart now does nothing but raise a warning.
  • Made 'root' argument to cherrypy.quickstart optional (to make tutorials easier, but it applies broadly).
  • Removed all calls to server.quickstart.
  • Property svn:eol-style set to native
Line 
1 """Manage HTTP servers with CherryPy."""
2
3 import socket
4 import warnings
5
6 import cherrypy
7 from cherrypy.lib import attributes
8
9 # We import * because we want to export check_port
10 # et al as attributes of this module.
11 from cherrypy.restsrv.servers import *
12
13
14 class Server(ServerAdapter):
15     """An adapter for an HTTP server.
16     
17     You can set attributes (like socket_host and socket_port)
18     on *this* object (which is probably cherrypy.server), and call
19     quickstart. For example:
20     
21         cherrypy.server.socket_port = 80
22         cherrypy.quickstart()
23     """
24    
25     socket_port = 8080
26    
27     _socket_host = '127.0.0.1'
28     def _get_socket_host(self):
29         return self._socket_host
30     def _set_socket_host(self, value):
31         if not value:
32             raise ValueError("Host values of '' or None are not allowed. "
33                              "Use '0.0.0.0' instead to listen on all active "
34                              "interfaces (INADDR_ANY).")
35         self._socket_host = value
36     socket_host = property(_get_socket_host, _set_socket_host,
37         doc="""The hostname or IP address on which to listen for connections.
38         
39         Host values may be any IPv4 or IPv6 address, or any valid hostname.
40         The string 'localhost' is a synonym for '127.0.0.1' (or '::1', if
41         your hosts file prefers IPv6). The string '0.0.0.0' is a special
42         IPv4 entry meaning "any active interface" (INADDR_ANY), and '::'
43         is the similar IN6ADDR_ANY for IPv6. The empty string or None are
44         not allowed.""")
45    
46     socket_file = ''
47     socket_queue_size = 5
48     socket_timeout = 10
49     shutdown_timeout = 5
50     protocol_version = 'HTTP/1.1'
51     reverse_dns = False
52     thread_pool = 10
53     max_request_header_size = 500 * 1024
54     max_request_body_size = 100 * 1024 * 1024
55     instance = None
56     ssl_certificate = None
57     ssl_private_key = None
58    
59     def __init__(self):
60         ServerAdapter.__init__(self, cherrypy.engine)
61    
62     def quickstart(self, server=None):
63         """This does nothing now and will be removed in 3.2."""
64         warnings.warn('quickstart does nothing now and will be removed in '
65                       '3.2. Call cherrypy.engine.start() instead.',
66                       DeprecationWarning)
67    
68     def httpserver_from_self(self, httpserver=None):
69         """Return a (httpserver, bind_addr) pair based on self attributes."""
70         if httpserver is None:
71             httpserver = self.instance
72         if httpserver is None:
73             from cherrypy import _cpwsgi
74             httpserver = _cpwsgi.CPWSGIServer()
75         if isinstance(httpserver, basestring):
76             httpserver = attributes(httpserver)()
77        
78         if self.socket_file:
79             return httpserver, self.socket_file
80        
81         host = self.socket_host
82         port = self.socket_port
83         return httpserver, (host, port)
84    
85     def start(self):
86         """Start the HTTP server."""
87         if not self.httpserver:
88             self.httpserver, self.bind_addr = self.httpserver_from_self()
89         ServerAdapter.start(self)
90     start.priority = 75
91    
92     def base(self):
93         """Return the base (scheme://host) for this server."""
94         if self.socket_file:
95             return self.socket_file
96        
97         host = self.socket_host
98         if host in ('0.0.0.0', '::'):
99             # 0.0.0.0 is INADDR_ANY and :: is IN6ADDR_ANY.
100             # Look up the host name, which should be the
101             # safest thing to spit out in a URL.
102             host = socket.gethostname()
103        
104         port = self.socket_port
105        
106         if self.ssl_certificate:
107             scheme = "https"
108             if port != 443:
109                 host += ":%s" % port
110         else:
111             scheme = "http"
112             if port != 80:
113                 host += ":%s" % port
114        
115         return "%s://%s" % (scheme, host)
116
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets