示例#1
0
 def worker_info(self, worker, info):
     info['sockets'] = sockets = []
     for server in worker.servers.get(self.name, ()):
         address = format_address(server.address)
         sockets.append({
             'address': format_address(server.address),
             'read_timeout': server.timeout,
             'concurrent_connections': server.concurrent_connections,
             'received_connections': server.received})
示例#2
0
 def worker_info(self, worker, info):
     info['sockets'] = sockets = []
     for server in worker.servers.get(self.name, ()):
         address = format_address(server.address)
         sockets.append({
             'address': format_address(server.address),
             'read_timeout': server.timeout,
             'concurrent_connections': server.concurrent_connections,
             'received_connections': server.received
         })
示例#3
0
    async def create_endpoint(self, **kw):
        """create the server endpoint.

        :return: a :class:`~asyncio.Future` called back when the server is
            serving the socket.
        """
        if hasattr(self, '_params'):
            address = self._params['address']
            sockets = self._params['sockets']
            del self._params
            try:
                transports = []
                loop = self._loop
                if sockets:
                    for sock in sockets:
                        transport, _ = await loop.create_datagram_endpoint(
                            self.create_protocol, sock=sock)
                        transports.append(transport)
                else:
                    transport, _ = await loop.create_datagram_endpoint(
                        self.create_protocol, local_addr=address)
                    transports.append(transport)
                self._transports = transports
                self._started = loop.time()
                for transport in self._transports:
                    address = transport.get_extra_info('sockname')
                    self.logger.info('%s serving on %s', self._name,
                                     format_address(address))
                self.fire_event('start')
            except Exception as exc:
                self.logger.exception('Error while starting UDP server')
                self.fire_event('start', exc=exc)
                self.fire_event('stop')
示例#4
0
    async def create_endpoint(self, **kw):
        """create the server endpoint.

        :return: a :class:`~asyncio.Future` called back when the server is
            serving the socket.
        """
        if hasattr(self, "_params"):
            address = self._params["address"]
            sockets = self._params["sockets"]
            del self._params
            try:
                transports = []
                loop = self._loop
                if sockets:
                    for sock in sockets:
                        transport, _ = await loop.create_datagram_endpoint(self.create_protocol, sock=sock)
                        transports.append(transport)
                else:
                    transport, _ = await loop.create_datagram_endpoint(self.create_protocol, local_addr=address)
                    transports.append(transport)
                self._transports = transports
                self._started = loop.time()
                for transport in self._transports:
                    address = transport.get_extra_info("sockname")
                    self.logger.info("%s serving on %s", self._name, format_address(address))
                self.fire_event("start")
            except Exception as exc:
                self.logger.exception("Error while starting UDP server")
                self.fire_event("start", exc=exc)
                self.fire_event("stop")
示例#5
0
    async def start_serving(self, backlog=100, sslcontext=None):
        """Start serving.

        :param backlog: Number of maximum connections
        :param sslcontext: optional SSLContext object.
        :return: a :class:`.Future` called back when the server is
            serving the socket.
        """
        assert not self._server
        if hasattr(self, "_params"):
            address = self._params["address"]
            sockets = self._params["sockets"]
            del self._params
            create_server = self._loop.create_server
            if sockets:
                server = None
                for sock in sockets:
                    srv = await create_server(self.create_protocol, sock=sock, backlog=backlog, ssl=sslcontext)
                    if server:
                        server.sockets.extend(srv.sockets)
                    else:
                        server = srv
            else:
                if isinstance(address, tuple):
                    server = await create_server(
                        self.create_protocol, host=address[0], port=address[1], backlog=backlog, ssl=sslcontext
                    )
                else:
                    raise NotImplementedError
            self._server = server
            self._started = self._loop.time()
            for sock in server.sockets:
                address = sock.getsockname()
                self.logger.info("%s serving on %s", self._name, format_address(address))
            self._loop.call_soon(self.fire_event, "start")
示例#6
0
文件: protocols.py 项目: cluo/pulsar
    def create_endpoint(self, **kw):
        '''create the server endpoint.

        :return: a :class:`~asyncio.Future` called back when the server is
            serving the socket.
        '''
        if hasattr(self, '_params'):
            address = self._params['address']
            sockets = self._params['sockets']
            del self._params
            try:
                transports = []
                if sockets:
                    for transport in sockets:
                        proto = self.create_protocol()
                        transports.append(transport(self._loop, proto))
                else:
                    loop = self._loop
                    transport, _ = yield from loop.create_datagram_endpoint(
                        self.protocol_factory, local_addr=address)
                    transports.append(transport)
                self._transports = transports
                self._started = self._loop.time()
                for transport in self._transports:
                    address = transport.get_extra_info('sockname')
                    self.logger.info('%s serving on %s', self._name,
                                     format_address(address))
                self.fire_event('start')
            except Exception as exc:
                self.logger.exception('Error while starting UDP server')
                self.fire_event('start', exc=exc)
                self.fire_event('stop')
示例#7
0
 def test_start_serving_ipv6(self):
     loop = get_event_loop()
     sockets = yield loop.start_serving(Protocol,'::1', 0)
     self.assertEqual(len(sockets), 1)
     sock = sockets[0]
     self.assertEqual(sock.family, socket.AF_INET6)
     address = sock.getsockname()
     faddress = format_address(address)
     self.assertEqual(faddress, '[::1]:%s' % address[1])
     loop.stop_serving(sock)
     self.assertTrue(is_socket_closed(sock))
示例#8
0
 def info(self):
     sockets = []
     up = int(self._loop.time() - self._started) if self._started else 0
     server = {"uptime_in_seconds": up, "sockets": sockets, "max_requests": self._max_requests}
     clients = {"requests_processed": self._requests_processed}
     if self._transports:
         for transport in self._transports:
             sock = transport.get_extra_info("socket")
             if sock:
                 sockets.append({"address": format_address(sock.getsockname())})
     return {"server": server, "clients": clients}
示例#9
0
 def info(self):
     sockets = []
     up = int(self._loop.time() - self._started) if self._started else 0
     server = {'uptime_in_seconds': up,
               'sockets': sockets,
               'max_requests': self._max_requests}
     clients = {'requests_processed': self._requests_processed}
     if self._transports:
         for transport in self._transports:
             sockets.append({
                 'address': format_address(transport._sock.getsockname())})
     return {'server': server,
             'clients': clients}
示例#10
0
 def info(self):
     sockets = []
     up = int(self._loop.time() - self._started) if self._started else 0
     server = {
         "pulsar_version": pulsar.__version__,
         "python_version": sys.version,
         "uptime_in_seconds": up,
         "sockets": sockets,
         "max_requests": self._max_requests,
     }
     clients = {"requests_processed": self._requests_processed}
     if self._transports:
         for transport in self._transports:
             sockets.append({"address": format_address(transport._sock.getsockname())})
     return {"server": server, "clients": clients}
示例#11
0
 def info(self):
     sockets = []
     up = int(self._loop.time() - self._started) if self._started else 0
     server = {'uptime_in_seconds': up,
               'sockets': sockets,
               'max_requests': self._max_requests,
               'keep_alive': self._keep_alive}
     clients = {'processed_clients': self._sessions,
                'connected_clients': len(self._concurrent_connections),
                'requests_processed': self._requests_processed}
     if self._server:
         for sock in self._server.sockets:
             sockets.append({
                 'address': format_address(sock.getsockname())})
     return {'server': server,
             'clients': clients}
示例#12
0
 def info(self):
     sockets = []
     up = int(self._loop.time() - self._started) if self._started else 0
     server = {
         "uptime_in_seconds": up,
         "sockets": sockets,
         "max_requests": self._max_requests,
         "keep_alive": self._keep_alive,
     }
     clients = {
         "processed_clients": self._sessions,
         "connected_clients": len(self._concurrent_connections),
         "requests_processed": self._requests_processed,
     }
     if self._server:
         for sock in self._server.sockets:
             sockets.append({"address": format_address(sock.getsockname())})
     return {"server": server, "clients": clients}
示例#13
0
    def start_serving(self, backlog=100, sslcontext=None):
        '''Start serving.

        :param backlog: Number of maximum connections
        :param sslcontext: optional SSLContext object.
        :return: a :class:`.Future` called back when the server is
            serving the socket.'''
        if hasattr(self, '_params'):
            address = self._params['address']
            sockets = self._params['sockets']
            del self._params
            create_server = self._loop.create_server
            try:
                if sockets:
                    server = None
                    for sock in sockets:
                        srv = yield From(
                            create_server(self.create_protocol,
                                          sock=sock,
                                          backlog=backlog,
                                          ssl=sslcontext))
                        if server:
                            server.sockets.extend(srv.sockets)
                        else:
                            server = srv
                else:
                    if isinstance(address, tuple):
                        server = yield From(
                            create_server(self.create_protocol,
                                          host=address[0],
                                          port=address[1],
                                          backlog=backlog,
                                          ssl=sslcontext))
                    else:
                        raise NotImplementedError
                self._server = server
                self._started = self._loop.time()
                for sock in server.sockets:
                    address = sock.getsockname()
                    self.logger.info('%s serving on %s', self._name,
                                     format_address(address))
                self.fire_event('start')
            except Exception as exc:
                self.fire_event('start', exc=exc)
示例#14
0
文件: protocols.py 项目: cluo/pulsar
    def start_serving(self, backlog=100, sslcontext=None):
        '''Start serving.

        :param backlog: Number of maximum connections
        :param sslcontext: optional SSLContext object.
        :return: a :class:`.Future` called back when the server is
            serving the socket.'''
        assert not self._server
        if hasattr(self, '_params'):
            address = self._params['address']
            sockets = self._params['sockets']
            del self._params
            create_server = self._loop.create_server
            try:
                if sockets:
                    server = None
                    for sock in sockets:
                        srv = yield from create_server(self.create_protocol,
                                                       sock=sock,
                                                       backlog=backlog,
                                                       ssl=sslcontext)
                        if server:
                            server.sockets.extend(srv.sockets)
                        else:
                            server = srv
                else:
                    if isinstance(address, tuple):
                        server = yield from create_server(self.create_protocol,
                                                          host=address[0],
                                                          port=address[1],
                                                          backlog=backlog,
                                                          ssl=sslcontext)
                    else:
                        raise NotImplementedError
                self._server = server
                self._started = self._loop.time()
                for sock in server.sockets:
                    address = sock.getsockname()
                    self.logger.info('%s serving on %s', self._name,
                                     format_address(address))
                self.fire_event('start')
            except Exception as exc:
                self.fire_event('start', exc=exc)
示例#15
0
 def __repr__(self):
     return '%s %s' % (self.__class__.__name__, format_address(
         self.address))
示例#16
0
def wsgi_environ(stream,
                 address,
                 client_address,
                 headers,
                 server_software=None,
                 https=False,
                 extra=None):
    protocol = stream.protocol()
    parser = stream.parser
    request_headers = stream.headers
    raw_uri = parser.get_url()
    request_uri = urlparse(raw_uri)
    #
    # http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.2
    # If Request-URI is an absoluteURI, the host is part of the Request-URI.
    # Any Host header field value in the request MUST be ignored
    if request_uri.scheme:
        url_scheme = request_uri.scheme
        host = request_uri.netloc
    else:
        url_scheme = 'https' if https else 'http'
        host = None
    #
    environ = {
        "wsgi.input": stream,
        "wsgi.errors": sys.stderr,
        "wsgi.version": (1, 0),
        "wsgi.run_once": False,
        "wsgi.multithread": False,
        "wsgi.multiprocess": False,
        "SERVER_SOFTWARE": server_software or pulsar.SERVER_SOFTWARE,
        "REQUEST_METHOD": native_str(parser.get_method()),
        "QUERY_STRING": parser.get_query_string(),
        "RAW_URI": raw_uri,
        "SERVER_PROTOCOL": protocol,
        "CONTENT_TYPE": ''
    }
    forward = client_address
    script_name = os.environ.get("SCRIPT_NAME", "")
    for header, value in request_headers:
        header = header.lower()
        if header in HOP_HEADERS:
            headers[header] = value
        if header == 'x-forwarded-for':
            forward = value
        elif header == "x-forwarded-protocol" and value == "ssl":
            url_scheme = "https"
        elif header == "x-forwarded-ssl" and value == "on":
            url_scheme = "https"
        elif header == "host" and not host:
            host = value
        elif header == "script_name":
            script_name = value
        elif header == "content-type":
            environ['CONTENT_TYPE'] = value
            continue
        elif header == "content-length":
            environ['CONTENT_LENGTH'] = value
            continue
        key = 'HTTP_' + header.upper().replace('-', '_')
        environ[key] = value
    environ['wsgi.url_scheme'] = url_scheme
    if url_scheme == 'https':
        environ['HTTPS'] = 'on'
    if is_string(forward):
        # we only took the last one
        # http://en.wikipedia.org/wiki/X-Forwarded-For
        if forward.find(",") >= 0:
            forward = forward.rsplit(",", 1)[1].strip()
        remote = forward.split(":")
        if len(remote) < 2:
            remote.append('80')
    else:
        remote = forward
    environ['REMOTE_ADDR'] = remote[0]
    environ['REMOTE_PORT'] = str(remote[1])
    if not host and protocol == 'HTTP/1.0':
        host = format_address(address)
    if host:
        host = host_and_port_default(url_scheme, host)
        environ['SERVER_NAME'] = socket.getfqdn(host[0])
        environ['SERVER_PORT'] = host[1]
    path_info = request_uri.path
    if path_info is not None:
        if script_name:
            path_info = path_info.split(script_name, 1)[1]
        environ['PATH_INFO'] = unquote(path_info)
    environ['SCRIPT_NAME'] = script_name
    if extra:
        environ.update(extra)
    return environ
示例#17
0
 def __repr__(self):
     return '%s %s' % (self.__class__.__name__,
                       format_address(self.address))
示例#18
0
 def _got_sockets(self, sockets):
     self._sock = sockets[0]
     self.logger.info('%s serving on %s', self._name,
                      format_address(self.address))
     return self
示例#19
0
 def test_format_address(self):
     self.assertRaises(ValueError, format_address, (1, ))
     self.assertRaises(ValueError, format_address, (1, 2, 3))
     self.assertRaises(ValueError, format_address, (1, 2, 3, 4, 5))
     self.assertEqual(format_address(1), '1')
示例#20
0
 def test_format_address(self):
     self.assertRaises(ValueError, format_address, (1,))
     self.assertRaises(ValueError, format_address, (1, 2, 3))
     self.assertRaises(ValueError, format_address, (1, 2, 3, 4, 5))
     self.assertEqual(format_address(1), '1')
示例#21
0
def wsgi_environ(stream, address, client_address, headers,
                 server_software=None, https=False, extra=None):
    protocol = stream.protocol()
    parser = stream.parser
    request_headers = stream.headers
    raw_uri = parser.get_url()
    request_uri = urlparse(raw_uri)
    #
    # http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.2
    # If Request-URI is an absoluteURI, the host is part of the Request-URI.
    # Any Host header field value in the request MUST be ignored
    if request_uri.scheme:
        url_scheme = request_uri.scheme
        host = request_uri.netloc
    else:
        url_scheme = 'https' if https else 'http'
        host = None
    #
    environ = {"wsgi.input": stream,
               "wsgi.errors": sys.stderr,
               "wsgi.version": (1, 0),
               "wsgi.run_once": False,
               "wsgi.multithread": False,
               "wsgi.multiprocess": False,
               "SERVER_SOFTWARE": server_software or pulsar.SERVER_SOFTWARE,
               "REQUEST_METHOD": native_str(parser.get_method()),
               "QUERY_STRING": parser.get_query_string(),
               "RAW_URI": raw_uri,
               "SERVER_PROTOCOL": protocol,
               "CONTENT_TYPE": ''}
    forward = client_address
    script_name = os.environ.get("SCRIPT_NAME", "")
    for header, value in request_headers:
        header = header.lower()
        if header in HOP_HEADERS:
            headers[header] = value
        if header == 'x-forwarded-for':
            forward = value
        elif header == "x-forwarded-protocol" and value == "ssl":
            url_scheme = "https"
        elif header == "x-forwarded-ssl" and value == "on":
            url_scheme = "https"
        elif header == "host" and not host:
            host = value
        elif header == "script_name":
            script_name = value
        elif header == "content-type":
            environ['CONTENT_TYPE'] = value
            continue
        elif header == "content-length":
            environ['CONTENT_LENGTH'] = value
            continue
        key = 'HTTP_' + header.upper().replace('-', '_')
        environ[key] = value
    environ['wsgi.url_scheme'] = url_scheme
    if url_scheme == 'https':
        environ['HTTPS'] = 'on'
    if is_string(forward):
        # we only took the last one
        # http://en.wikipedia.org/wiki/X-Forwarded-For
        if forward.find(",") >= 0:
            forward = forward.rsplit(",", 1)[1].strip()
        remote = forward.split(":")
        if len(remote) < 2:
            remote.append('80')
    else:
        remote = forward
    environ['REMOTE_ADDR'] = remote[0]
    environ['REMOTE_PORT'] = str(remote[1])
    if not host and protocol == 'HTTP/1.0':
        host = format_address(address)
    if host:
        host = host_and_port_default(url_scheme, host)
        environ['SERVER_NAME'] = socket.getfqdn(host[0])
        environ['SERVER_PORT'] = host[1]
    path_info = parser.get_path()
    if path_info is not None:
        if script_name:
            path_info = path_info.split(script_name, 1)[1]
        environ['PATH_INFO'] = unquote(path_info)
    environ['SCRIPT_NAME'] = script_name
    if extra:
        environ.update(extra)
    return environ
示例#22
0
 def _got_sockets(self, sockets):
     self._sock = sockets[0]
     self.logger.info('%s serving on %s', self._name,
                      format_address(self.address))
     return self
示例#23
0
文件: server.py 项目: luffyhwl/pulsar
def wsgi_environ(
    stream, parser, request_headers, address, client_address, headers, server_software=None, https=False, extra=None
):
    """Build the WSGI Environment dictionary

    :param stream: a wsgi stream object
    :param parser: pulsar HTTP parser
    :param request_headers: headers of request
    :param address: server address
    :param client_address: client address
    :param headers: container for response headers
    """
    protocol = http_protocol(parser)
    raw_uri = parser.get_url()
    request_uri = urlparse(raw_uri)
    #
    # http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.2
    # If Request-URI is an absoluteURI, the host is part of the Request-URI.
    # Any Host header field value in the request MUST be ignored
    if request_uri.scheme:
        url_scheme = request_uri.scheme
        host = request_uri.netloc
    else:
        url_scheme = "https" if https else "http"
        host = None
    #
    environ = {
        "wsgi.input": stream,
        "wsgi.errors": sys.stderr,
        "wsgi.version": (1, 0),
        "wsgi.run_once": False,
        "wsgi.multithread": False,
        "wsgi.multiprocess": False,
        "SERVER_SOFTWARE": server_software or pulsar.SERVER_SOFTWARE,
        "REQUEST_METHOD": native_str(parser.get_method()),
        "QUERY_STRING": parser.get_query_string(),
        "RAW_URI": raw_uri,
        "SERVER_PROTOCOL": protocol,
        "CONTENT_TYPE": "",
    }
    forward = client_address
    script_name = os.environ.get("SCRIPT_NAME", "")
    for header, value in request_headers:
        header = header.lower()
        if header in HOP_HEADERS:
            headers[header] = value
        if header == "x-forwarded-for":
            forward = value
        elif header == "x-forwarded-protocol" and value == "ssl":
            url_scheme = "https"
        elif header == "x-forwarded-ssl" and value == "on":
            url_scheme = "https"
        elif header == "host" and not host:
            host = value
        elif header == "script_name":
            script_name = value
        elif header == "content-type":
            environ["CONTENT_TYPE"] = value
            continue
        elif header == "content-length":
            environ["CONTENT_LENGTH"] = value
            continue
        key = "HTTP_" + header.upper().replace("-", "_")
        environ[key] = value
    environ["wsgi.url_scheme"] = url_scheme
    if url_scheme == "https":
        environ["HTTPS"] = "on"
    if isinstance(forward, str):
        # we only took the last one
        # http://en.wikipedia.org/wiki/X-Forwarded-For
        if forward.find(",") >= 0:
            forward = forward.rsplit(",", 1)[1].strip()
        remote = forward.split(":")
        if len(remote) < 2:
            remote.append("80")
    else:
        remote = forward
    environ["REMOTE_ADDR"] = remote[0]
    environ["REMOTE_PORT"] = str(remote[1])
    if not host and protocol == "HTTP/1.0":
        host = format_address(address)
    if host:
        h = host_and_port_default(url_scheme, host)
        environ["SERVER_NAME"] = socket.getfqdn(h[0]) if h[0] else "0.0.0.0"
        environ["SERVER_PORT"] = h[1]
    path_info = request_uri.path
    if path_info is not None:
        if script_name:
            path_info = path_info.split(script_name, 1)[1]
        environ["PATH_INFO"] = unquote(path_info)
    environ["SCRIPT_NAME"] = script_name
    if extra:
        environ.update(extra)
    return environ