Beispiel #1
0
    def __init__(self, config, threads=10):
        """Takes an httpy.Config object.

        The thread count should probably be exposed to configuration, but I
        still want to hold out the possibility of swapping out a different base
        server, which may change the meaning of that parameter.

        """

        # Install some basic API.
        # =======================

        self.config = config
        mode = os.environ.get('HTTPY_MODE', 'deployment')
        self.debug_mode = mode == 'debugging'
        self.deploy_mode = mode == 'deployment'
        self.devel_mode = mode == 'development'

        # Set up signal handling so we can shut down cleanly.
        # ===================================================

        for sig in STOP_SIGNALS:
            signal.signal(sig, self.stop)

        # Create a task dispatcher.
        # =========================

        td = ThreadedTaskDispatcher()
        td.setThreadCount(threads)
        ServerBase.__init__(self,
                            config.ip,
                            config.port,
                            start=False,
                            task_dispatcher=td)
Beispiel #2
0
    def __init__(self, config, responder):
        """Takes an IConfig and an IResponder.
        """

        self.config = config
        self.responder = responder


        # Set up signal handling so we can shut down cleanly.
        # ===================================================

        for sig in STOP_SIGNALS:
            signal.signal(sig, self.stop)


        # Satisfy ServerBase requirements.
        # ================================

        asyncore.dispatcher.__init__(self)
        self.adj = default_adj
        if (not WINDOWS) and (self.config.sockfam == socket.AF_UNIX):
            # The default adjustment doesn't apply here, and triggers an error.
            self.adj.socket_options = []
        self.verbose = False
        task_dispatcher = ThreadedTaskDispatcher()
        task_dispatcher.setThreadCount(self.config.threads)
        self.task_dispatcher = task_dispatcher
        self.logger = logger
Beispiel #3
0
    def __init__(self, responder, address, threads, uid):
        """Takes an IResponder.
        """

        #validate_input(responder, address, threads, uid)

        self.responder = responder
        self.address = address
        if address[0] in ('/', '.'):
            self.sockfam = socket.AF_UNIX
        else:
            self.sockfam = socket.AF_INET
        self.threads = threads
        self.uid = uid

        # Set up signal handling so we can shut down cleanly.
        # ===================================================

        for sig in STOP_SIGNALS:
            signal.signal(sig, self.stop)

        # Satisfy ServerBase requirements.
        # ================================

        asyncore.dispatcher.__init__(self)
        self.adj = default_adj
        if (not WINDOWS) and (self.sockfam == socket.AF_UNIX):
            # The default adjustment doesn't apply here, and triggers an error.
            self.adj.socket_options = []
        self.verbose = False
        task_dispatcher = ThreadedTaskDispatcher()
        task_dispatcher.setThreadCount(self.threads)
        self.task_dispatcher = task_dispatcher
        self.logger = logging.getLogger(self.__class__.__name__)
Beispiel #4
0
    def __init__(self, config):
        """Takes an httpy.Config object.
        """

        # Install some basic API.
        # =======================

        self.config = config

        self.debug_mode  = self.config.mode == 'debugging'
        self.deploy_mode = self.config.mode == 'deployment'
        self.devel_mode  = self.config.mode == 'development'

        self.framework = self.get_framework()
        self.apps = self.get_apps()


        # Set up signal handling so we can shut down cleanly.
        # ===================================================

        for sig in STOP_SIGNALS:
            signal.signal(sig, self.stop)


        # Satisfy ServerBase requirements.
        # ================================

        asyncore.dispatcher.__init__(self)
        self.adj = default_adj
        if self.config.sockfam == socket.AF_UNIX:
            # The default adjustment doesn't apply here, and triggers an error.
            self.adj.socket_options = []
        self.verbose = False
        task_dispatcher = ThreadedTaskDispatcher()
        task_dispatcher.setThreadCount(self.config.threads)
        self.task_dispatcher = task_dispatcher
        self.logger = logger


        # Bind to an address.
        # ===================

        self.create_socket(config.sockfam, socket.SOCK_STREAM)
        self.set_reuse_addr()
        if config.sockfam == socket.AF_UNIX:
            if os.path.exists(config.address):
                os.unlink(config.address)
                logger.debug("unlinking stale socket")

        timeout = time.time() + 10
        while 1:
            if time.time() < timeout:
                try:
                    self.bind(config.address)
                    break
                except socket.error, err:
                    time.sleep(0.5)
            else:
                raise err
Beispiel #5
0
    def __init__(self, config, threads=10):
        """Takes an httpy.ServerConfig object.

        The thread count should probably be exposed to configuration, but I
        still want to hold out the possibility of swapping out a different base
        server, which may change the meaning of that parameter.

        """

        self.config = config
        td = ThreadedTaskDispatcher()
        td.setThreadCount(threads)
        self.mtimes = {}

        ServerBase.__init__( self
                           , config.ip
                           , config.port
                           , start=0
                           , task_dispatcher=td
                            )
Beispiel #6
0
    def __init__(self, config, threads=10):
        """Takes an httpy.ServerServerConfig object.

        We stick verbosity in the environment so that httpy.log can use it. The
        thread count should probably be exposed to configuration, but I still
        want to hold out the possibility of swapping out a different base
        server, which may change the meaning of that parameter.

        """

        self.config = config
        os.environ["HTTPY_VERBOSITY"] = str(config.verbosity)
        td = ThreadedTaskDispatcher()
        td.setThreadCount(threads)
        self.mtimes = {}

        ServerBase.__init__( self
                           , config.ip
                           , config.port
                           , start=0
                           , task_dispatcher=td
                            )
Beispiel #7
0
import sys
import traceback
import unittest

from time import time
from types import StringType
from StringIO import StringIO
from threading import Thread, Event

from httpy._zope.server.adjustments import Adjustments
from httpy._zope.server.ftp.server import FTPServer, status_messages
from httpy._zope.server.ftp.tests import demofs
from httpy._zope.server.taskthreads import ThreadedTaskDispatcher
from httpy._zope.server.tests.asyncerror import AsyncoreErrorHook

td = ThreadedTaskDispatcher()

LOCALHOST = '127.0.0.1'
SERVER_PORT = 0  # Set these port numbers to 0 to auto-bind, or
CONNECT_TO_PORT = 0  # use specific numbers to inspect using TCPWatch.

my_adj = Adjustments()


def retrlines(ftpconn, cmd):
    res = []
    ftpconn.retrlines(cmd, res.append)
    return ''.join(res)


class Tests(unittest.TestCase, AsyncoreErrorHook):
Beispiel #8
0
class HTTPServer(ServerBase):
    """This is a generic HTTP Server."""

    channel_class = HTTPServerChannel
    SERVER_IDENT = 'httpy._zope.server.http'

    def executeRequest(self, task):
        """Execute an HTTP request."""
        # This is a default implementation, meant to be overridden.
        body = "The HTTP server is running!\r\n" * 10
        task.response_headers['Content-Type'] = 'text/plain'
        task.response_headers['Content-Length'] = str(len(body))
        task.write(body)


if __name__ == '__main__':

    from httpy._zope.server.taskthreads import ThreadedTaskDispatcher
    td = ThreadedTaskDispatcher()
    td.setThreadCount(4)
    HTTPServer('', 8080, task_dispatcher=td)

    try:
        import asyncore
        while 1:
            asyncore.poll(5)

    except KeyboardInterrupt:
        print 'shutting down...'
        td.shutdown()