Package cherrypy :: Module _cpserver
[hide private]
[frames] | no frames]

Source Code for Module cherrypy._cpserver

  1  """Manage HTTP servers with CherryPy.""" 
  2   
  3  import warnings 
  4   
  5  import cherrypy 
  6  from cherrypy.lib import attributes 
  7  from cherrypy._cpcompat import basestring, py3k 
  8   
  9  # We import * because we want to export check_port 
 10  # et al as attributes of this module. 
 11  from cherrypy.process.servers import * 
 12   
 13   
14 -class Server(ServerAdapter):
15 16 """An adapter for an HTTP server. 17 18 You can set attributes (like socket_host and socket_port) 19 on *this* object (which is probably cherrypy.server), and call 20 quickstart. For example:: 21 22 cherrypy.server.socket_port = 80 23 cherrypy.quickstart() 24 """ 25 26 socket_port = 8080 27 """The TCP port on which to listen for connections.""" 28 29 _socket_host = '127.0.0.1' 30
31 - def _get_socket_host(self):
32 return self._socket_host
33
34 - def _set_socket_host(self, value):
35 if value == '': 36 raise ValueError("The empty string ('') is not an allowed value. " 37 "Use '0.0.0.0' instead to listen on all active " 38 "interfaces (INADDR_ANY).") 39 self._socket_host = value
40 socket_host = property( 41 _get_socket_host, 42 _set_socket_host, 43 doc="""The hostname or IP address on which to listen for connections. 44 45 Host values may be any IPv4 or IPv6 address, or any valid hostname. 46 The string 'localhost' is a synonym for '127.0.0.1' (or '::1', if 47 your hosts file prefers IPv6). The string '0.0.0.0' is a special 48 IPv4 entry meaning "any active interface" (INADDR_ANY), and '::' 49 is the similar IN6ADDR_ANY for IPv6. The empty string or None are 50 not allowed.""") 51 52 socket_file = None 53 """If given, the name of the UNIX socket to use instead of TCP/IP. 54 55 When this option is not None, the `socket_host` and `socket_port` options 56 are ignored.""" 57 58 socket_queue_size = 5 59 """The 'backlog' argument to socket.listen(); specifies the maximum number 60 of queued connections (default 5).""" 61 62 socket_timeout = 10 63 """The timeout in seconds for accepted connections (default 10).""" 64 65 accepted_queue_size = -1 66 """The maximum number of requests which will be queued up before 67 the server refuses to accept it (default -1, meaning no limit).""" 68 69 accepted_queue_timeout = 10 70 """The timeout in seconds for attempting to add a request to the 71 queue when the queue is full (default 10).""" 72 73 shutdown_timeout = 5 74 """The time to wait for HTTP worker threads to clean up.""" 75 76 protocol_version = 'HTTP/1.1' 77 """The version string to write in the Status-Line of all HTTP responses, 78 for example, "HTTP/1.1" (the default). Depending on the HTTP server used, 79 this should also limit the supported features used in the response.""" 80 81 thread_pool = 10 82 """The number of worker threads to start up in the pool.""" 83 84 thread_pool_max = -1 85 """The maximum size of the worker-thread pool. Use -1 to indicate no limit. 86 """ 87 88 max_request_header_size = 500 * 1024 89 """The maximum number of bytes allowable in the request headers. 90 If exceeded, the HTTP server should return "413 Request Entity Too Large". 91 """ 92 93 max_request_body_size = 100 * 1024 * 1024 94 """The maximum number of bytes allowable in the request body. If exceeded, 95 the HTTP server should return "413 Request Entity Too Large".""" 96 97 instance = None 98 """If not None, this should be an HTTP server instance (such as 99 CPWSGIServer) which cherrypy.server will control. Use this when you need 100 more control over object instantiation than is available in the various 101 configuration options.""" 102 103 ssl_context = None 104 """When using PyOpenSSL, an instance of SSL.Context.""" 105 106 ssl_certificate = None 107 """The filename of the SSL certificate to use.""" 108 109 ssl_certificate_chain = None 110 """When using PyOpenSSL, the certificate chain to pass to 111 Context.load_verify_locations.""" 112 113 ssl_private_key = None 114 """The filename of the private key to use with SSL.""" 115 116 if py3k: 117 ssl_module = 'builtin' 118 """The name of a registered SSL adaptation module to use with 119 the builtin WSGI server. Builtin options are: 'builtin' (to 120 use the SSL library built into recent versions of Python). 121 You may also register your own classes in the 122 wsgiserver.ssl_adapters dict.""" 123 else: 124 ssl_module = 'pyopenssl' 125 """The name of a registered SSL adaptation module to use with the 126 builtin WSGI server. Builtin options are 'builtin' (to use the SSL 127 library built into recent versions of Python) and 'pyopenssl' (to 128 use the PyOpenSSL project, which you must install separately). You 129 may also register your own classes in the wsgiserver.ssl_adapters 130 dict.""" 131 132 statistics = False 133 """Turns statistics-gathering on or off for aware HTTP servers.""" 134 135 nodelay = True 136 """If True (the default since 3.1), sets the TCP_NODELAY socket option.""" 137 138 wsgi_version = (1, 0) 139 """The WSGI version tuple to use with the builtin WSGI server. 140 The provided options are (1, 0) [which includes support for PEP 3333, 141 which declares it covers WSGI version 1.0.1 but still mandates the 142 wsgi.version (1, 0)] and ('u', 0), an experimental unicode version. 143 You may create and register your own experimental versions of the WSGI 144 protocol by adding custom classes to the wsgiserver.wsgi_gateways dict.""" 145
146 - def __init__(self):
147 self.bus = cherrypy.engine 148 self.httpserver = None 149 self.interrupt = None 150 self.running = False
151
152 - def httpserver_from_self(self, httpserver=None):
153 """Return a (httpserver, bind_addr) pair based on self attributes.""" 154 if httpserver is None: 155 httpserver = self.instance 156 if httpserver is None: 157 from cherrypy import _cpwsgi_server 158 httpserver = _cpwsgi_server.CPWSGIServer(self) 159 if isinstance(httpserver, basestring): 160 # Is anyone using this? Can I add an arg? 161 httpserver = attributes(httpserver)(self) 162 return httpserver, self.bind_addr
163
164 - def start(self):
165 """Start the HTTP server.""" 166 if not self.httpserver: 167 self.httpserver, self.bind_addr = self.httpserver_from_self() 168 ServerAdapter.start(self)
169 start.priority = 75 170
171 - def _get_bind_addr(self):
172 if self.socket_file: 173 return self.socket_file 174 if self.socket_host is None and self.socket_port is None: 175 return None 176 return (self.socket_host, self.socket_port)
177
178 - def _set_bind_addr(self, value):
179 if value is None: 180 self.socket_file = None 181 self.socket_host = None 182 self.socket_port = None 183 elif isinstance(value, basestring): 184 self.socket_file = value 185 self.socket_host = None 186 self.socket_port = None 187 else: 188 try: 189 self.socket_host, self.socket_port = value 190 self.socket_file = None 191 except ValueError: 192 raise ValueError("bind_addr must be a (host, port) tuple " 193 "(for TCP sockets) or a string (for Unix " 194 "domain sockets), not %r" % value)
195 bind_addr = property( 196 _get_bind_addr, 197 _set_bind_addr, 198 doc='A (host, port) tuple for TCP sockets or ' 199 'a str for Unix domain sockets.') 200
201 - def base(self):
202 """Return the base (scheme://host[:port] or sock file) for this server. 203 """ 204 if self.socket_file: 205 return self.socket_file 206 207 host = self.socket_host 208 if host in ('0.0.0.0', '::'): 209 # 0.0.0.0 is INADDR_ANY and :: is IN6ADDR_ANY. 210 # Look up the host name, which should be the 211 # safest thing to spit out in a URL. 212 import socket 213 host = socket.gethostname() 214 215 port = self.socket_port 216 217 if self.ssl_certificate: 218 scheme = "https" 219 if port != 443: 220 host += ":%s" % port 221 else: 222 scheme = "http" 223 if port != 80: 224 host += ":%s" % port 225 226 return "%s://%s" % (scheme, host)
227