Robert Brewer avatar Robert Brewer committed f8f032c

Final cleanups for [698] and #321. cherrypy.server is now an instance of cherrpy._cpserver.Server, instead of a module.

Comments (0)

Files changed (7)

cherrypy/__init__.py

 
 from _cperror import *
 import config
-import server
-
-# Use a flag to indicate the state of the cherrypy application server.
-# 0 = Not started
-# None = In process of starting
-# 1 = Started, ready to receive requests
-_appserver_state = 0
-_httpserver = None
-_httpserverclass = None
-_interrupt = None
+import _cpserver
+server = _cpserver.Server()
 
 codecoverage = False
 

cherrypy/_cpserver.py

+"""
+Copyright (c) 2004, CherryPy Team (team@cherrypy.org)
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, 
+are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright notice, 
+      this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above copyright notice, 
+      this list of conditions and the following disclaimer in the documentation 
+      and/or other materials provided with the distribution.
+    * Neither the name of the CherryPy Team nor the names of its contributors 
+      may be used to endorse or promote products derived from this software 
+      without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+"""
+
+"""
+Main CherryPy module:
+    - Creates a server
+"""
+
+import cgi
+import threading
+import time
+import warnings
+
+import cherrypy
+from cherrypy import _cphttptools
+from cherrypy.lib import autoreload, profiler
+
+# Use a flag to indicate the state of the application server.
+STOPPED = 0
+STARTING = None
+STARTED = 1
+
+seen_threads = {}
+
+_missing = object()
+
+
+class Server(object):
+    
+    def __init__(self):
+        self.state = STOPPED
+        
+        self.httpserver = None
+        self.httpserverclass = None
+        self.interrupt = None
+        
+        # Set some special attributes for adding hooks
+        self.onStartServerList = []
+        self.onStartThreadList = []
+        self.onStopServerList = []
+        self.onStopThreadList = []
+    
+    def start(self, initOnly=False, serverClass=_missing):
+        """Main function. MUST be called from the main thread.
+        
+        Set initOnly to True to keep this function from blocking.
+        Set serverClass to None to skip starting any HTTP server.
+        """
+        self.state = STARTING
+        self.interrupt = None
+        
+        conf = cherrypy.config.get
+        
+        if serverClass is _missing:
+            serverClass = conf("server.class", _missing)
+        if serverClass is _missing:
+            import _cpwsgi
+            serverClass = _cpwsgi.WSGIServer
+        elif serverClass and isinstance(serverClass, basestring):
+            # Dynamically load the class from the given string
+            serverClass = cherrypy._cputil.attributes(serverClass)
+        
+        self.blocking = not initOnly
+        self.httpserverclass = serverClass
+        
+        # Autoreload, but check serverClass. If None, we're not starting
+        # our own webserver, and therefore could do Very Bad Things when
+        # autoreload calls sys.exit.
+        if serverClass is not None:
+            defaultOn = (conf("server.environment") == "development")
+            if conf('autoreload.on', defaultOn):
+                try:
+                    autoreload.main(self._start)
+                except KeyboardInterrupt:
+                    cherrypy.log("<Ctrl-C> hit: shutting down autoreloader", "HTTP")
+                return
+        
+        self._start()
+    
+    def _start(self):
+        # Output config options to log
+        if cherrypy.config.get("server.logConfigOptions", True):
+            cherrypy.config.outputConfigMap()
+        
+        try:
+            configure()
+            
+            for func in cherrypy.server.onStartServerList:
+                func()
+            self.start_http_server()
+            self.state = STARTED
+            
+            if self.blocking:
+                # Block forever (wait for KeyboardInterrupt or SystemExit).
+                while True:
+                    time.sleep(.1)
+                    if self.interrupt:
+                        raise self.interrupt
+        except KeyboardInterrupt:
+            cherrypy.log("<Ctrl-C> hit: shutting down server", "HTTP")
+            self.stop()
+        except SystemExit:
+            cherrypy.log("SystemExit raised: shutting down server", "HTTP")
+            self.stop()
+    
+    def start_http_server(self, blocking=True):
+        """Start the requested HTTP server."""
+        if self.httpserver is not None:
+            msg = ("You seem to have an HTTP server still running."
+                   "Please call server.stop_http_server() "
+                   "before continuing.")
+            warnings.warn(msg)
+        
+        if self.httpserverclass is None:
+            return
+        
+        if cherrypy.config.get('server.socketPort'):
+            host = cherrypy.config.get('server.socketHost')
+            port = cherrypy.config.get('server.socketPort')
+            
+            wait_for_free_port(host, port)
+            
+            if not host:
+                host = 'localhost'
+            onWhat = "http://%s:%s/" % (host, port)
+        else:
+            onWhat = "socket file: %s" % cherrypy.config.get('server.socketFile')
+        
+        # Instantiate the server.
+        self.httpserver = self.httpserverclass()
+        
+        # HTTP servers MUST be started in a new thread, so that the
+        # main thread persists to receive KeyboardInterrupt's. This
+        # wrapper traps an interrupt in the http server's main thread
+        # and shutdowns CherryPy.
+        def _start_http():
+            try:
+                self.httpserver.start()
+            except (KeyboardInterrupt, SystemExit), exc:
+                self.interrupt = exc
+        threading.Thread(target=_start_http).start()
+        
+        if blocking:
+            self.wait_for_http_ready()
+        
+        cherrypy.log("Serving HTTP on %s" % onWhat, 'HTTP')
+    
+    def wait_for_http_ready(self):
+        if self.httpserverclass is not None:
+            while not getattr(self.httpserver, "ready", True):
+                time.sleep(.1)
+            
+            # Wait for port to be occupied
+            if cherrypy.config.get('server.socketPort'):
+                host = cherrypy.config.get('server.socketHost')
+                port = cherrypy.config.get('server.socketPort')
+                wait_for_occupied_port(host, port)
+    
+    def request(self, clientAddress, remoteHost, requestLine,
+                headers, rfile, scheme="http"):
+        """request(clientAddress, remoteHost, requestLine, headers, rfile, scheme="http")
+        
+        clientAddress: the (IP address, port) of the client
+        remoteHost: the IP address of the client
+        requestLine: "<HTTP method> <url?qs> HTTP/<version>",
+                e.g. "GET /main?abc=123 HTTP/1.1"
+        headers: a list of (key, value) tuples
+        rfile: a file-like object from which to read the HTTP request body
+        scheme: either "http" or "https"; defaults to "http"
+        """
+        if self.state == STOPPED:
+            raise cherrypy.NotReady("The CherryPy server has stopped.")
+        elif self.state == STARTING:
+            raise cherrypy.NotReady("The CherryPy server could not start.")
+        
+        threadID = threading._get_ident()
+        if threadID not in seen_threads:
+            
+            if cherrypy.codecoverage:
+                from cherrypy.lib import covercp
+                covercp.start()
+            
+            i = len(seen_threads) + 1
+            seen_threads[threadID] = i
+            
+            for func in self.onStartThreadList:
+                func(i)
+        
+        if cherrypy.profiler:
+            cherrypy.profiler.run(_cphttptools.Request, clientAddress, remoteHost,
+                                  requestLine, headers, rfile, scheme)
+        else:
+            _cphttptools.Request(clientAddress, remoteHost,
+                                 requestLine, headers, rfile, scheme)
+    
+    def stop(self):
+        """Stop, including any HTTP servers."""
+        self.stop_http_server()
+        self.state = STOPPED
+        cherrypy.log("CherryPy shut down", "HTTP")
+    
+    def stop_http_server(self):
+        """Stop the HTTP server."""
+        try:
+            httpstop = self.httpserver.stop
+        except AttributeError:
+            pass
+        else:
+            # httpstop() MUST block until the server is *truly* stopped.
+            httpstop()
+            cherrypy.log("HTTP Server shut down", "HTTP")
+        
+        for thread_ident, i in seen_threads.iteritems():
+            for func in self.onStopThreadList:
+                func(i)
+        seen_threads.clear()
+        
+        for func in self.onStopServerList:
+            func()
+        
+        self.httpserver = None
+    
+    def restart(self):
+        """Restart, including any HTTP servers."""
+        self.stop()
+        for func in self.onStartServerList:
+            func()
+        self.state = STARTED
+        self.start_http_server()
+    
+    def wait(self):
+        """Block the caller until ready to receive requests."""
+        while not self.ready:
+            time.sleep(.1)
+    
+    def _is_ready(self):
+        return bool(self.state == STARTED)
+    ready = property(_is_ready, doc="Return True if the server is ready to receive requests, False otherwise.")
+    
+    def start_with_callback(self, func, args=None, kwargs=None,
+                            serverClass=_missing):
+        """Start, then callback the given func in a new thread."""
+        if args is None:
+            args = ()
+        if kwargs is None:
+            kwargs = {}
+        args = (func,) + args
+        
+        def _callback(func, *args, **kwargs):
+            self.wait()
+            func(*args, **kwargs)
+        threading.Thread(target=_callback, args=args, kwargs=kwargs).start()
+        
+        self.start(serverClass=serverClass)
+
+
+def configure():
+    """Perform one-time actions to prepare the CherryPy core."""
+    if cherrypy.codecoverage:
+        from cherrypy.lib import covercp
+        covercp.start()
+    
+    conf = cherrypy.config.get
+    # TODO: config.checkConfigOptions()
+    
+    # If sessions are stored in files and we
+    # use threading, we need a lock on the file
+    if (conf('server.threadPool') > 1
+        and conf('session.storageType') == 'file'):
+        cherrypy._sessionFileLock = threading.RLock()
+    
+    # set cgi.maxlen which will limit the size of POST request bodies
+    cgi.maxlen = conf('server.maxRequestSize')
+    
+    # Set up the profiler if requested.
+    if conf("profiling.on", False):
+        ppath = conf("profiling.path", "")
+        cherrypy.profiler = profiler.Profiler(ppath)
+    else:
+        cherrypy.profiler = None
+    
+    # Initialize the built in filters
+    cherrypy._cputil._cpInitDefaultFilters()
+    cherrypy._cputil._cpInitUserDefinedFilters()
+
+
+def check_port(host, port):
+    """Raise an error if the given port is not free on the given host."""
+    if not host:
+        host = 'localhost'
+    
+    import socket
+    try:
+        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        s.connect((host, int(port)))
+        s.close()
+        raise IOError("Port %s is in use on %s; perhaps the previous "
+                      "server did not shut down properly." %
+                      (repr(port), repr(host)))
+    except socket.error:
+        pass
+
+def wait_for_free_port(host, port):
+    """Wait for the specified port to become free (drop requests)."""
+    if not host:
+        host = 'localhost'
+    
+    for trial in xrange(50):
+        try:
+            check_port(host, port)
+        except IOError:
+            # Give the old server thread time to free the port.
+            time.sleep(.1)
+        else:
+            return
+    
+    cherrypy.log("Port %s not free on %s" % (repr(port), repr(host)), 'HTTP')
+    raise cherrypy.NotReady("Port not free.")
+
+def wait_for_occupied_port(host, port):
+    """Wait for the specified port to become active (receive requests)."""
+    if not host:
+        host = 'localhost'
+    
+    for trial in xrange(50):
+        try:
+            check_port(host, port)
+        except IOError:
+            return
+        else:
+            time.sleep(.1)
+    
+    cherrypy.log("Port %s not bound on %s" % (repr(port), repr(host)), 'HTTP')
+    raise cherrypy.NotReady("Port not bound.")

cherrypy/server.py

-"""
-Copyright (c) 2004, CherryPy Team (team@cherrypy.org)
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without modification, 
-are permitted provided that the following conditions are met:
-
-    * Redistributions of source code must retain the above copyright notice, 
-      this list of conditions and the following disclaimer.
-    * Redistributions in binary form must reproduce the above copyright notice, 
-      this list of conditions and the following disclaimer in the documentation 
-      and/or other materials provided with the distribution.
-    * Neither the name of the CherryPy Team nor the names of its contributors 
-      may be used to endorse or promote products derived from this software 
-      without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
-ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
-WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
-DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
-SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
-CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
-OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
-OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-"""
-
-"""
-Main CherryPy module:
-    - Creates a server
-"""
-
-import cgi
-import threading
-import time
-import warnings
-
-import cherrypy
-from cherrypy import _cphttptools
-from cherrypy.lib import autoreload, profiler
-
-
-# Set some special attributes for adding hooks
-onStartServerList = []
-onStartThreadList = []
-onStopServerList = []
-onStopThreadList = []
-
-
-_missing = object()
-
-def start(initOnly=False, serverClass=_missing):
-    """Main function. MUST be called from the main thread.
-    
-    Set initOnly to True to keep this function from blocking.
-    Set serverClass to None to skip starting any HTTP server.
-    """
-    # This duplicates the line in start_app_server on purpose.
-    cherrypy._appserver_state = None
-    cherrypy._interrupt = None
-    
-    conf = cherrypy.config.get
-    
-    if serverClass is _missing:
-        serverClass = conf("server.class", _missing)
-    if serverClass is _missing:
-        import _cpwsgi
-        serverClass = _cpwsgi.WSGIServer
-    elif serverClass and isinstance(serverClass, basestring):
-        # Dynamically load the class from the given string
-        serverClass = cherrypy._cputil.attributes(serverClass)
-    
-    # Autoreload, but check serverClass. If None, we're not starting
-    # our own webserver, and therefore could do Very Bad Things when
-    # autoreload calls sys.exit.
-    if serverClass is not None:
-        defaultOn = (conf("server.environment") == "development")
-        if conf('autoreload.on', defaultOn):
-            try:
-                autoreload.main(_start, (initOnly, serverClass))
-            except KeyboardInterrupt:
-                cherrypy.log("<Ctrl-C> hit: shutting down autoreloader", "HTTP")
-            return
-    
-    _start(initOnly, serverClass)
-
-def _start(initOnly, serverClass):
-    # This duplicates the line in start_app_server on purpose.
-    cherrypy._appserver_state = None
-    
-    cherrypy._httpserverclass = serverClass
-    conf = cherrypy.config.get
-    
-    try:
-        if cherrypy.codecoverage:
-            from cherrypy.lib import covercp
-            covercp.start()
-        
-        # Output config options to log
-        if conf("server.logConfigOptions", True):
-            cherrypy.config.outputConfigMap()
-        # TODO: config.checkConfigOptions()
-        
-        # If sessions are stored in files and we
-        # use threading, we need a lock on the file
-        if (conf('server.threadPool') > 1
-            and conf('session.storageType') == 'file'):
-            cherrypy._sessionFileLock = threading.RLock()
-        
-        # set cgi.maxlen which will limit the size of POST request bodies
-        cgi.maxlen = conf('server.maxRequestSize')
-        
-        # Set up the profiler if requested.
-        if conf("profiling.on", False):
-            ppath = conf("profiling.path", "")
-            cherrypy.profiler = profiler.Profiler(ppath)
-        else:
-            cherrypy.profiler = None
-        
-        # Initialize the built in filters
-        cherrypy._cputil._cpInitDefaultFilters()
-        cherrypy._cputil._cpInitUserDefinedFilters()
-        
-        start_app_server()
-        start_http_server()
-        wait_until_ready()
-        
-        if not initOnly:
-            # Block forever (wait for KeyboardInterrupt or SystemExit).
-            while True:
-                time.sleep(.1)
-                if cherrypy._interrupt:
-                    raise cherrypy._interrupt
-    except KeyboardInterrupt:
-        cherrypy.log("<Ctrl-C> hit: shutting down server", "HTTP")
-        stop()
-    except SystemExit:
-        cherrypy.log("SystemExit raised: shutting down server", "HTTP")
-        stop()
-
-def start_app_server():
-    """Start the CherryPy core."""
-    # Use a flag to indicate the state of the cherrypy application server.
-    # 0 = Not started
-    # None = In process of starting
-    # 1 = Started, ready to receive requests
-    cherrypy._appserver_state = None
-    
-    # Call the functions from cherrypy.server.onStartServerList
-    for func in cherrypy.server.onStartServerList:
-        func()
-    
-    cherrypy._appserver_state = 1
-
-def start_http_server(serverClass=None):
-    """Start the requested HTTP server."""
-    if serverClass is None:
-        serverClass = cherrypy._httpserverclass
-    if serverClass is None:
-        return
-    
-    if cherrypy._httpserver is not None:
-        msg = ("You seem to have an HTTP server still running."
-               "Please call cherrypy.server.stop_http_server() "
-               "before continuing.")
-        warnings.warn(msg)
-    
-    if cherrypy.config.get('server.socketPort'):
-        host = cherrypy.config.get('server.socketHost')
-        port = cherrypy.config.get('server.socketPort')
-        
-        wait_for_free_port(host, port)
-        
-        if not host:
-            host = 'localhost'
-        onWhat = "http://%s:%s/" % (host, port)
-    else:
-        onWhat = "socket file: %s" % cherrypy.config.get('server.socketFile')
-    cherrypy.log("Serving HTTP on %s" % onWhat, 'HTTP')
-    
-    # Instantiate the server.
-    cherrypy._httpserver = serverClass()
-    
-    # HTTP servers MUST be started in a new thread, so that the
-    # main thread persists to receive KeyboardInterrupt's. This
-    # wrapper traps an interrupt in the http server's main thread
-    # and shutdowns CherryPy.
-    def _start_http():
-        try:
-            cherrypy._httpserver.start()
-        except (KeyboardInterrupt, SystemExit), exc:
-            cherrypy._interrupt = exc
-    threading.Thread(target=_start_http).start()
-
-
-seen_threads = {}
-
-def request(clientAddress, remoteHost, requestLine, headers, rfile, scheme="http"):
-    """request(clientAddress, remoteHost, requestLine, headers, rfile, scheme="http")
-    
-    clientAddress: the (IP address, port) of the client
-    remoteHost: the IP address of the client
-    requestLine: "<HTTP method> <url?qs> HTTP/<version>",
-            e.g. "GET /main?abc=123 HTTP/1.1"
-    headers: a list of (key, value) tuples
-    rfile: a file-like object from which to read the HTTP request body
-    scheme: either "http" or "https"; defaults to "http"
-    """
-    if cherrypy._appserver_state == 0:
-        raise cherrypy.NotReady("No thread has called cherrypy.server.start().")
-    elif cherrypy._appserver_state == None:
-        raise cherrypy.NotReady("cherrypy.server.start() encountered errors.")
-    
-    threadID = threading._get_ident()
-    if threadID not in seen_threads:
-        
-        if cherrypy.codecoverage:
-            from cherrypy.lib import covercp
-            covercp.start()
-        
-        i = len(seen_threads) + 1
-        seen_threads[threadID] = i
-        # Call the functions from cherrypy.server.onStartThreadList
-        for func in cherrypy.server.onStartThreadList:
-            func(i)
-    
-    if cherrypy.profiler:
-        cherrypy.profiler.run(_cphttptools.Request, clientAddress, remoteHost,
-                              requestLine, headers, rfile, scheme)
-    else:
-        _cphttptools.Request(clientAddress, remoteHost,
-                             requestLine, headers, rfile, scheme)
-
-def stop():
-    """Stop CherryPy and any HTTP servers it started."""
-    stop_http_server()
-    stop_app_server()
-
-def stop_app_server():
-    """Stop CherryPy."""
-    cherrypy._appserver_state = 0
-    cherrypy.log("CherryPy shut down", "HTTP")
-
-def stop_http_server():
-    """Stop the HTTP server."""
-    try:
-        httpstop = cherrypy._httpserver.stop
-    except AttributeError:
-        pass
-    else:
-        # httpstop() MUST block until the server is *truly* stopped.
-        httpstop()
-        cherrypy.log("HTTP Server shut down", "HTTP")
-    
-    # Call the functions from cherrypy.server.onStopThreadList
-    for thread_ident, i in seen_threads.iteritems():
-        for func in cherrypy.server.onStopThreadList:
-            func(i)
-    seen_threads.clear()
-    
-    # Call the functions from cherrypy.server.onStopServerList
-    for func in cherrypy.server.onStopServerList:
-        func()
-    
-    cherrypy._httpserver = None
-
-def restart():
-    """Restart CherryPy (and any HTTP servers it started)."""
-    stop()
-    start_app_server()
-    start_http_server()
-    wait_until_ready()
-
-def wait_until_ready():
-    """Block the caller until CherryPy is ready to receive requests."""
-    
-    # Wait for app to start up
-    while cherrypy._appserver_state != 1:
-        time.sleep(.1)
-    
-    # Wait for HTTP server to start up
-    if cherrypy._httpserverclass is not None:
-        while not getattr(cherrypy._httpserver, "ready", None):
-            time.sleep(.1)
-        
-        # Wait for port to be occupied
-        if cherrypy.config.get('server.socketPort'):
-            host = cherrypy.config.get('server.socketHost')
-            port = cherrypy.config.get('server.socketPort')
-            wait_for_occupied_port(host, port)
-
-def check_port(host, port):
-    """Raise an error if the given port is not free on the given host."""
-    
-    import socket
-    try:
-        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-        s.connect((host, int(port)))
-        s.close()
-        raise IOError("Port %s is in use on %s; perhaps the previous "
-                      "server did not shut down properly." % (port, host))
-    except socket.error:
-        pass
-
-def wait_for_free_port(host, port):
-    """Wait for the specified port to become free (drop requests)."""
-    for trial in xrange(50):
-        try:
-            check_port(host, port)
-        except IOError:
-            # Give the old server thread time to free the port.
-            time.sleep(.1)
-        else:
-            return
-    
-    cherrypy.log("Port %s not free" % port, 'HTTP')
-    raise cherrypy.NotReady("Port not free.")
-
-def wait_for_occupied_port(host, port):
-    """Wait for the specified port to become active (receive requests)."""
-    for trial in xrange(50):
-        try:
-            check_port(host, port)
-        except IOError:
-            return
-        else:
-            time.sleep(.1)
-    
-    cherrypy.log("Port %s not bound" % port, 'HTTP')
-    raise cherrypy.NotReady("Port not bound.")
-
-def start_with_callback(func, args=None, kwargs=None, serverClass=_missing):
-    """Start CherryPy, then callback the given func in a new thread."""
-    if args is None:
-        args = ()
-    if kwargs is None:
-        kwargs = {}
-    args = (func,) + args
-    threading.Thread(target=_callback_intermediary, args=args, kwargs=kwargs).start()
-    cherrypy.server.start(serverClass=serverClass)
-
-def _callback_intermediary(func, *args, **kwargs):
-    wait_until_ready()
-    func(*args, **kwargs)

cherrypy/test/helper.py

         # 2) stop the HTTP request (if any) and ignore further assertions.
         cherrypy.root._cpOnError = onerror
         
-        if cherrypy._httpserver is None:
+        if cherrypy.server.httpserver is None:
             self._getRequest(url, headers, method, body)
         else:
             webtest.WebCase.getPage(self, url, headers, method, body)
                                         serverClass=server)
 
 def _run_test_suite_thread(moduleNames, conf):
-    cherrypy.server.wait_until_ready()
+    cherrypy.server.wait()
     for testmod in moduleNames:
         # Must run each module in a separate suite,
         # because each module uses/overwrites cherrypy globals.
     cherrypy.server.start_with_callback(_test_main_thread, serverClass=server)
 
 def _test_main_thread():
-    cherrypy.server.wait_until_ready()
+    cherrypy.server.wait()
     cherrypy._cputil._cpInitDefaultFilters()
     try:
         webtest.main()

cherrypy/test/test_core.py

             
             import cherrypy
             # streamResponse should be True for this path.
-            if cherrypy._httpserver is None:
+            if cherrypy.server.httpserver is None:
                 self.assertRaises(ValueError, self.getPage,
                                   "/error/page_streamed")
             else:
         self.getPage("/maxrequestsize/index")
         self.assertBody("OK")
         
-        if cherrypy._httpserver.__class__.__name__ == "WSGIServer":
+        httpcls = cherrypy.server.httpserverclass
+        if httpcls and httpcls.__name__ == "WSGIServer":
             cherrypy.config.update({'server.maxRequestHeaderSize': 10})
             self.getPage("/maxrequestsize/index")
             self.assertStatus("413 Request Entity Too Large")
         self.getPage('/maxrequestsize/upload', h, "POST", b)
         self.assertBody('Size: 5')
         
-        if cherrypy._httpserver.__class__.__name__ == "WSGIServer":
+        if cherrypy.server.httpserverclass.__name__ == "WSGIServer":
             cherrypy.config.update({
                 '/maxrequestsize': {'server.maxRequestBodySize': 3}})
             self.getPage('/maxrequestsize/upload', h, "POST", b)

cherrypy/test/test_gzip_filter.py

             # readable page, since 1) the gzip header is already set,
             # and 2) we may have already written some of the body.
             # The fix is to never stream yields when using gzip.
-            if cherrypy._httpserver is None:
+            if cherrypy.server.httpserver is None:
                 self.assertRaises(IndexError, self.getPage,
                                   '/noshow_stream',
                                   [("Accept-Encoding", "gzip")])

cherrypy/test/test_states.py

         
         # Test server start
         cherrypy.server.start(True, self.serverClass)
-        self.assertEqual(cherrypy._appserver_state, 1)
+        self.assertEqual(cherrypy.server.state, 1)
         
         if self.serverClass:
             host = cherrypy.config.get('server.socketHost')
             port = cherrypy.config.get('server.socketPort')
-            self.assertRaises(IOError, cherrypy.server.check_port, host, port)
+            self.assertRaises(IOError, cherrypy._cpserver.check_port, host, port)
         
         self.getPage("/")
         self.assertBody("Hello World")
         
         # Test server stop
         cherrypy.server.stop()
-        self.assertEqual(cherrypy._appserver_state, 0)
+        self.assertEqual(cherrypy.server.state, 0)
         
         # Once the server has stopped, we should get a NotReady error again.
         self.assertRaises(cherrypy.NotReady, self.getPage, "/")
         
         # Test server restart from this thread
         cherrypy.server.restart()
-        self.assertEqual(cherrypy._appserver_state, 1)
+        self.assertEqual(cherrypy.server.state, 1)
         self.getPage("/")
         self.assertBody("Hello World")
         
         # Test server restart from inside a page handler
         self.getPage("/restart")
-        self.assertEqual(cherrypy._appserver_state, 1)
+        self.assertEqual(cherrypy.server.state, 1)
         self.assertBody("app was restarted succesfully")
         
         cherrypy.server.stop()
-        self.assertEqual(cherrypy._appserver_state, 0)
+        self.assertEqual(cherrypy.server.state, 0)
     
     def test_2_KeyboardInterrupt(self):
         if self.serverClass:
             
             # Raise a keyboard interrupt in the HTTP server's main thread.
             def interrupt():
-                cherrypy.server.wait_until_ready()
-                cherrypy._httpserver.interrupt = KeyboardInterrupt
+                cherrypy.server.wait()
+                cherrypy.server.httpserver.interrupt = KeyboardInterrupt
             threading.Thread(target=interrupt).start()
             
             # We must start the server in this, the main thread
             cherrypy.server.start(False, self.serverClass)
             # Time passes...
-            self.assertEqual(cherrypy._httpserver, None)
-            self.assertEqual(cherrypy._appserver_state, 0)
+            self.assertEqual(cherrypy.server.httpserver, None)
+            self.assertEqual(cherrypy.server.state, 0)
             self.assertRaises(cherrypy.NotReady, self.getPage, "/")
             
             # Raise a keyboard interrupt in a page handler; on multithreaded
             # This should raise a BadStatusLine error, since the worker
             # thread will just die without writing a response.
             def interrupt():
-                cherrypy.server.wait_until_ready()
+                cherrypy.server.wait()
                 from httplib import BadStatusLine
                 self.assertRaises(BadStatusLine, self.getPage, "/ctrlc")
             threading.Thread(target=interrupt).start()
             
             cherrypy.server.start(False, self.serverClass)
             # Time passes...
-            self.assertEqual(cherrypy._httpserver, None)
-            self.assertEqual(cherrypy._appserver_state, 0)
+            self.assertEqual(cherrypy.server.httpserver, None)
+            self.assertEqual(cherrypy.server.state, 0)
             self.assertRaises(cherrypy.NotReady, self.getPage, "/")
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.