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

root/trunk/cherrypy/_cpserver.py

Revision 1994 (checked in by fumanchu, 1 week ago)

Fix for #803 (run CP under Google App Engine).

  • Property svn:eol-style set to native
Line 
1 """Manage HTTP servers with CherryPy."""
2
3 import warnings
4
5 import cherrypy
6 from cherrypy.lib import attributes
7
8 # We import * because we want to export check_port
9 # et al as attributes of this module.
10 from cherrypy.process.servers import *
11
12
13 class Server(ServerAdapter):
14     """An adapter for an HTTP server.
15     
16     You can set attributes (like socket_host and socket_port)
17     on *this* object (which is probably cherrypy.server), and call
18     quickstart. For example:
19     
20         cherrypy.server.socket_port = 80
21         cherrypy.quickstart()
22     """
23    
24     socket_port = 8080
25    
26     _socket_host = '127.0.0.1'
27     def _get_socket_host(self):
28         return self._socket_host
29     def _set_socket_host(self, value):
30         if not value:
31             raise ValueError("Host values of '' or None are not allowed. "
32                              "Use '0.0.0.0' instead to listen on all active "
33                              "interfaces (INADDR_ANY).")
34         self._socket_host = value
35     socket_host = property(_get_socket_host, _set_socket_host,
36         doc="""The hostname or IP address on which to listen for connections.
37         
38         Host values may be any IPv4 or IPv6 address, or any valid hostname.
39         The string 'localhost' is a synonym for '127.0.0.1' (or '::1', if
40         your hosts file prefers IPv6). The string '0.0.0.0' is a special
41         IPv4 entry meaning "any active interface" (INADDR_ANY), and '::'
42         is the similar IN6ADDR_ANY for IPv6. The empty string or None are
43         not allowed.""")
44    
45     socket_file = ''
46     socket_queue_size = 5
47     socket_timeout = 10
48     shutdown_timeout = 5
49     protocol_version = 'HTTP/1.1'
50     reverse_dns = False
51     thread_pool = 10
52     max_request_header_size = 500 * 1024
53     max_request_body_size = 100 * 1024 * 1024
54     instance = None
55     ssl_certificate = None
56     ssl_private_key = None
57     nodelay = True
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_server
74             httpserver = _cpwsgi_server.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             import socket
103             host = socket.gethostname()
104        
105         port = self.socket_port
106        
107         if self.ssl_certificate:
108             scheme = "https"
109             if port != 443:
110                 host += ":%s" % port
111         else:
112             scheme = "http"
113             if port != 80:
114                 host += ":%s" % port
115        
116         return "%s://%s" % (scheme, host)
117
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets