Пример #1
0
    def test_create_socket(self):

        # testing various options

        # regular socket
        sock = create_socket('0.0.0.0')
        try:
            _, port = sock.getsockname()
            self.assertNotEqual(port, 0)
        finally:
            sock.close()

        # fd-based socket
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind(('0.0.0.0', 0))
        _, port = sock.getsockname()

        sock2 = create_socket('fd://%d' % sock.fileno())
        try:
            _, port2 = sock2.getsockname()
            self.assertEqual(port2, port)
        finally:
            sock2.close()
            sock.close()

        # unix socket
        fd, path = tempfile.mkstemp()
        os.close(fd)
        sock = create_socket('unix://%s' % path)
        try:
            self.assertEqual('//' + path, sock.getsockname())
        finally:
            sock.close()
            os.remove(path)
Пример #2
0
    def test_create_socket(self):

        # testing various options

        # regular socket
        sock = create_socket('0.0.0.0')
        try:
            _, port = sock.getsockname()
            self.assertNotEqual(port, 0)
        finally:
            sock.close()

        # fd-based socket
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind(('0.0.0.0', 0))
        _, port = sock.getsockname()

        sock2 = create_socket('fd://%d' % sock.fileno())
        try:
            _, port2 = sock2.getsockname()
            self.assertEqual(port2, port)
        finally:
            sock2.close()
            sock.close()

        # unix socket
        fd, path = tempfile.mkstemp()
        os.close(fd)
        sock = create_socket('unix://%s' % path)
        try:
            self.assertEqual('//' + path, sock.getsockname())
        finally:
            sock.close()
            os.remove(path)
Пример #3
0
 def __init__(self, listener, application=None, backlog=None):
     eventlet.monkey_patch()
     host, port = listener
     self.socket = create_socket(host, port, self.address_family,
                                 self.socket_type, backlog=backlog)
     self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     self.application = application
Пример #4
0
    def __init__(self, listener, application=None, backlog=None,
                 spawn='default', log='default', handler_class=None,
                 environ=None, socket_type=None, address_family=None,
                 graceful_timeout=None, disable_monkeypatch=False,
                 **ssl_args):
        if address_family:
            self.address_family = address_family
        if socket_type:
            self.socket_type = socket_type
        if handler_class:
            self.handler_class = handler_class

        host, port = listener
        self.socket = create_socket(host, port, self.address_family,
                                    self.socket_type, backlog=backlog)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server_address = self.socket.getsockname()

        if graceful_timeout is not None:
            self.stop_timeout = graceful_timeout
            gevent_signal(signal.SIGTERM, self.stop)

        super(Server, self).__init__(self.socket, application, None, spawn,
                                     log, self.handler_class, environ,
                                     **ssl_args)
Пример #5
0
    def __init__(self,
                 listener,
                 application=None,
                 backlog=None,
                 spawn='default',
                 log='default',
                 handler_class=None,
                 environ=None,
                 socket_type=socket.SOCK_STREAM,
                 address_family=socket.AF_INET,
                 graceful_timeout=None,
                 **ssl_args):
        host, port = listener
        self.socket = create_socket(host,
                                    port,
                                    self.address_family,
                                    self.socket_type,
                                    backlog=backlog)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server_address = self.socket.getsockname()

        if graceful_timeout is not None:
            self.stop_timeout = graceful_timeout
            gevent_signal(signal.SIGTERM, self.stop)

        super(Server, self).__init__(self.socket, application, None, spawn,
                                     log, handler_class, environ, **ssl_args)
Пример #6
0
    def __init__(self,
                 listener,
                 application=None,
                 backlog=None,
                 spawn='default',
                 log='default',
                 handler_class=None,
                 environ=None,
                 socket_type=socket.SOCK_STREAM,
                 address_family=socket.AF_INET,
                 **ssl_args):
        monkey.noisy = False
        monkey.patch_all()
        self.address_family = address_family
        self.socket_type = socket_type

        host, port = listener
        self.handler_class = CustomWebSocketHandler
        self.socket = create_socket(host,
                                    port,
                                    self.address_family,
                                    self.socket_type,
                                    backlog=backlog)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server_address = self.socket.getsockname()
        super(Server, self).__init__(self.socket, application, None, spawn,
                                     log, handler_class, environ, **ssl_args)

        gevent.signal(signal.SIGHUP, partial(self.stop, timeout=10000))
Пример #7
0
    def __init__(self, *args, **kwargs):
        address_family = kwargs.pop('address_family', socket.AF_INET)
        socket_type = kwargs.pop('socket_type', socket.SOCK_STREAM)
        backlog = kwargs.pop('backlog', 2048)

        listener = args[0]
        if isinstance(listener, tuple):
            host, port = listener
            _socket = create_socket(host, port, address_family,
                                    socket_type, backlog=backlog)
            _socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            args = [_socket] + list(args[1:])
        else:
            # it's already a socket..
            host, port = listener.getsockname()

        # socketio makes the assumption that listener is a host/port
        # tuple, which is a false assumption, it can be a socket.
        # it uses listener in its constructor to set the policy server
        #
        # Let's set it ourselves here afterwards.
        old_policy_server = kwargs.pop('policy_server', True)
        kwargs['policy_server'] = False
        super(Server, self).__init__(*args, **kwargs)

        if old_policy_server:
            policylistener = kwargs.pop('policy_listener', (host, 10843))
            self.policy_server = FlashPolicyServer(policylistener)
Пример #8
0
 def __init__(self, listener, application=None, backlog=None,
              socket_type=None, address_family=None, **kw):
     host, port = listener
     self.app = application
     self.sock = create_socket(host, port, address_family,
                               socket_type, backlog=backlog)
     self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Пример #9
0
 def __init__(self,
              listener,
              application=None,
              backlog=None,
              spawn='default',
              log='default',
              handler_class=None,
              environ=None,
              socket_type=socket.SOCK_STREAM,
              address_family=socket.AF_INET,
              **ssl_args):
     self.address_family = address_family
     self.socket_type = socket_type
     monkey.noisy = False
     monkey.patch_all()
     host, port = listener
     self.socket = create_socket(host,
                                 port,
                                 self.address_family,
                                 self.socket_type,
                                 backlog=backlog)
     self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     self.server_address = self.socket.getsockname()
     log = None
     super(Server, self).__init__(self.socket, application, None, spawn,
                                  log, handler_class, environ, **ssl_args)
Пример #10
0
 def __init__(self, listener, application=None, backlog=None):
     eventlet.monkey_patch()
     host, port = listener
     self.socket = create_socket(host,
                                 port,
                                 self.address_family,
                                 self.socket_type,
                                 backlog=backlog)
     self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     self.application = application
Пример #11
0
 def __init__(self, listener, application=None, backlog=None,
              spawn='default', log='default', handler_class=None,
              environ=None, socket_type=socket.SOCK_STREAM,
              address_family=socket.AF_INET, **ssl_args):
     host, port = listener
     self.socket = create_socket(host, port, self.address_family,
                                 self.socket_type, backlog=backlog)
     self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     self.server_address = self.socket.getsockname()
     super(Server, self).__init__(self.socket, application, None, spawn,
                                  log, handler_class, environ, **ssl_args)
Пример #12
0
    def __init__(self, server_address, app, bind_and_activate=True,
                 backlog=2048):
        BaseServer.__init__(self, server_address, self.handler_class)
        self.set_app(app)

        host, port = self.server_address = server_address
        self.socket = create_socket(host, port, self.address_family,
                                    self.socket_type, backlog=backlog)
        self.byfd = host.startswith('fd://')
        if bind_and_activate:
            self.server_bind()
            self.server_activate()
Пример #13
0
 def __init__(self, listener, application=None, backlog=None,
              spawn='default', log='default', handler_class=None,
              environ=None, **ssl_args):
     monkey.noisy = False
     monkey.patch_all()
     host, port = listener
     self.socket = create_socket(host, port, self.address_family,
                                 self.socket_type, backlog=backlog)
     self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     self.server_address = self.socket.getsockname()
     log = None
     super(Server, self).__init__(self.socket, application, None, spawn,
                                  log, handler_class, environ, **ssl_args)
Пример #14
0
 def __init__(self, listener, application=None, backlog=2048,
              socket_type=socket.SOCK_STREAM,
              address_family=socket.AF_INET,
              disable_monkeypatch=False):
     self.address_family = address_family
     self.socket_type = socket_type
     if not disable_monkeypatch:
         eventlet.monkey_patch()
     host, port = listener
     self.socket = create_socket(host, port, self.address_family,
                                 self.socket_type, backlog=backlog)
     self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     self.application = application
Пример #15
0
 def __init__(self,
              listener,
              application=None,
              backlog=None,
              socket_type=None,
              address_family=None):
     host, port = listener
     self.app = application
     self.sock = create_socket(host,
                               port,
                               address_family,
                               socket_type,
                               backlog=backlog)
     self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Пример #16
0
    def __init__(self, server_address, app, bind_and_activate=True,
                 backlog=2048, socket_type=socket.SOCK_STREAM,
                 address_family=socket.AF_INET):
        self.address_family = address_family
        self.socket_type = socket_type
        BaseServer.__init__(self, server_address, self.handler_class)
        self.set_app(app)

        host, port = self.server_address = server_address
        self.socket = create_socket(host, port,
                                    family=self.address_family,
                                    type=self.socket_type,
                                    backlog=backlog)
        self.byfd = host.startswith('fd://')
        if bind_and_activate:
            self.server_bind()
            self.server_activate()
Пример #17
0
    def __init__(self, listener, application=None, backlog=None,
                 spawn='default', log='default', handler_class=None,
                 environ=None, socket_type=socket.SOCK_STREAM,
                 address_family=socket.AF_INET, **ssl_args):
        monkey.noisy = False
        monkey.patch_all()
        self.address_family = address_family
        self.socket_type = socket_type

        host, port = listener
        self.handler_class = CustomWebSocketHandler
        self.socket = create_socket(host, port, self.address_family,
                                    self.socket_type, backlog=backlog)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server_address = self.socket.getsockname()
        super(Server, self).__init__(self.socket, application, None, spawn,
                                     log, handler_class, environ, **ssl_args)

        gevent.signal(signal.SIGHUP, partial(self.stop, timeout = 10000))
Пример #18
0
    def __init__(self, listener, application=None, backlog=2048,
                 socket_type=socket.SOCK_STREAM, spawn='default',
                 handler_class=None, environ=None,
                 log='default', address_family=socket.AF_INET, **config):
        self.address_family = address_family
        self.socket_type = socket_type
        host, port = listener
        self.socket = create_socket(host, port, self.address_family,
                                    self.socket_type, backlog=backlog)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.application = application
        self.sockets = {}
        if 'namespace' in config:
            self.resource = config.pop('namespace', 'socket.io')
        else:
            self.resource = config.pop('resource', 'socket.io')

        self.transports = config.pop('transports', None)
        self.policy_server = None
        WSGIServer.__init__(self, self.socket, application, None, spawn,
                            log, self.handler_class, environ, **config)
Пример #19
0
    def __init__(self, listener, application=None, backlog=None,
                 spawn='default', log='default', handler_class=None,
                 environ=None, socket_type=socket.SOCK_STREAM,
                 address_family=socket.AF_INET, graceful_timeout=None,
                 **ssl_args):
        self.address_family = address_family
        self.socket_type = socket_type

        host, port = listener
        self.handler_class = WebSocketHandler
        self.socket = create_socket(host, port, self.address_family,
                                    self.socket_type, backlog=backlog)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server_address = self.socket.getsockname()

        if graceful_timeout is not None:
            self.stop_timeout = graceful_timeout
            gevent_signal(signal.SIGTERM, self.stop)

        super(Server, self).__init__(self.socket, application, None, spawn,
                                     log, handler_class, environ, **ssl_args)
Пример #20
0
    def __init__(
        self, listener, application=None, backlog=2048,
        socket_type=socket.SOCK_STREAM, address_family=socket.AF_INET
    ):
        """
        Create socket.
        """
        host, port = listener

        self.application = application
        self.address_family = address_family
        self.socket_type = socket_type
        self.socket = create_socket(
            host,
            port,
            self.address_family,
            self.socket_type,
            backlog=backlog
        )
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server_address = self.socket.getsockname()
Пример #21
0
    def __init__(self,
                 listener,
                 application=None,
                 backlog=2048,
                 socket_type=socket.SOCK_STREAM,
                 address_family=socket.AF_INET):
        """
        Create socket.
        """
        host, port = listener

        self.application = application
        self.address_family = address_family
        self.socket_type = socket_type
        self.socket = create_socket(host,
                                    port,
                                    self.address_family,
                                    self.socket_type,
                                    backlog=backlog)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server_address = self.socket.getsockname()
Пример #22
0
    def __init__(self,
                 server_address,
                 app,
                 bind_and_activate=True,
                 backlog=2048,
                 socket_type=socket.SOCK_STREAM,
                 address_family=socket.AF_INET):
        self.address_family = address_family
        self.socket_type = socket_type
        BaseServer.__init__(self, server_address, self.handler_class)
        self.set_app(app)

        host, port = self.server_address = server_address
        self.socket = create_socket(host,
                                    port,
                                    family=self.address_family,
                                    type=self.socket_type,
                                    backlog=backlog)
        self.byfd = host.startswith('fd://')
        if bind_and_activate:
            self.server_bind()
            self.server_activate()