Python gevent.server() Examples

The following are 30 code examples of gevent.server(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module gevent , or try the search function .
Example #1
Source File: serverclient.py    From gipc with MIT License 23 votes vote down vote up
def server():
    ss = StreamServer(('localhost', PORT), serve).serve_forever() 
Example #2
Source File: main.py    From powerpool with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def main():
    parser = argparse.ArgumentParser(description='Run powerpool!')
    parser.add_argument('config', type=argparse.FileType('r'),
                        help='yaml configuration file to run with')
    parser.add_argument('-d', '--dump-config', action="store_true",
                        help='print the result of the YAML configuration file and exit')
    parser.add_argument('-s', '--server-number', type=int, default=0,
                        help='increase the configued server_number by this much')
    args = parser.parse_args()

    # override those defaults with a loaded yaml config
    raw_config = yaml.load(args.config) or {}
    if args.dump_config:
        import pprint
        pprint.pprint(raw_config)
        exit(0)
    PowerPool.from_raw_config(raw_config, vars(args)).start() 
Example #3
Source File: pywsgi.py    From PhonePi_SampleServer with MIT License 6 votes vote down vote up
def _main():
    # Provisional main handler, for quick tests, not production
    # usage.
    from gevent import monkey; monkey.patch_all()

    import argparse
    import importlib

    parser = argparse.ArgumentParser()
    parser.add_argument("app", help="dotted name of WSGI app callable [module:callable]")
    parser.add_argument("-b", "--bind",
                        help="The socket to bind",
                        default=":8080")

    args = parser.parse_args()

    module_name, app_name = args.app.split(':')
    module = importlib.import_module(module_name)
    app = getattr(module, app_name)
    bind = args.bind

    server = WSGIServer(bind, app)
    server.serve_forever() 
Example #4
Source File: pywsgi.py    From PhonePi_SampleServer with MIT License 6 votes vote down vote up
def update_environ(self):
        """
        Called before the first request is handled to fill in WSGI environment values.

        This includes getting the correct server name and port.
        """
        address = self.address
        if isinstance(address, tuple):
            if 'SERVER_NAME' not in self.environ:
                try:
                    name = socket.getfqdn(address[0])
                except socket.error:
                    name = str(address[0])
                if PY3 and not isinstance(name, str):
                    name = name.decode('ascii') # python 2 pylint:disable=redefined-variable-type
                self.environ['SERVER_NAME'] = name
            self.environ.setdefault('SERVER_PORT', str(address[1]))
        else:
            self.environ.setdefault('SERVER_NAME', '')
            self.environ.setdefault('SERVER_PORT', '') 
Example #5
Source File: api.py    From AIT-Core with MIT License 6 votes vote down vote up
def __init__ (self, listener, pktbuf, defn=None):
        """Creates a new UdpTelemetryServer.

        The server listens for UDP packets matching the given
        ``PacketDefinition`` *defn*.

        The *listener* is either a port on localhost, a tuple
        containing ``(hostname, port)``, or a
        ``gevent.socket.socket``.

        If the optional *defn* is not specified, the first
        ``PacketDefinition`` (alphabetical by name) in the default
        telemetry dictionary (i.e. ``tlm.getDefaultDict()``) is used.
        """
        if type(listener) is int:
            listener = ('127.0.0.1', listener)

        super(UdpTelemetryServer, self).__init__(listener)
        self._defn   = defn
        self._pktbuf = pktbuf 
Example #6
Source File: pywsgi.py    From satori with Apache License 2.0 6 votes vote down vote up
def update_environ(self):
        """
        Called before the first request is handled to fill in WSGI environment values.

        This includes getting the correct server name and port.
        """
        address = self.address
        if isinstance(address, tuple):
            if 'SERVER_NAME' not in self.environ:
                try:
                    name = socket.getfqdn(address[0])
                except socket.error:
                    name = str(address[0])
                if PY3 and not isinstance(name, str):
                    name = name.decode('ascii')
                self.environ['SERVER_NAME'] = name
            self.environ.setdefault('SERVER_PORT', str(address[1]))
        else:
            self.environ.setdefault('SERVER_NAME', '')
            self.environ.setdefault('SERVER_PORT', '') 
Example #7
Source File: server.py    From ReadableWebProxy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def main():
	print("Preloading cache directories")

	# print("Testing reload")
	# server.tree.tree.reloadTree()
	# print("Starting RPC server")
	try:
		run()

	except:
		# abort /hard/ if we exceptioned out of the main run.
		# This should (hopeully) cause the OS to terminate any
		# remaining threads.
		# As it is, I've been having issues with the main thread failing
		# with 'OSError: [Errno 24] Too many open files', killing the main thread
		# and leaving some of the amqp interface threads dangling.
		# Somehow, it's not being caught in the `except Exception:` handler
		# in run(). NFI how.
		import ctypes
		ctypes.string_at(0) 
Example #8
Source File: pywsgi.py    From PokemonGo-DesktopMap with MIT License 6 votes vote down vote up
def update_environ(self):
        """
        Called before the first request is handled to fill in WSGI environment values.

        This includes getting the correct server name and port.
        """
        address = self.address
        if isinstance(address, tuple):
            if 'SERVER_NAME' not in self.environ:
                try:
                    name = socket.getfqdn(address[0])
                except socket.error:
                    name = str(address[0])
                if PY3 and not isinstance(name, str):
                    name = name.decode('ascii')
                self.environ['SERVER_NAME'] = name
            self.environ.setdefault('SERVER_PORT', str(address[1]))
        else:
            self.environ.setdefault('SERVER_NAME', '')
            self.environ.setdefault('SERVER_PORT', '') 
Example #9
Source File: portforwarder.py    From web_develop with GNU General Public License v3.0 6 votes vote down vote up
def forward(source, dest, server):
    source_address = '%s:%s' % source.getpeername()[:2]
    dest_address = '%s:%s' % dest.getpeername()[:2]
    try:
        while True:
            try:
                data = source.recv(1024)
                log('%s->%s', source_address, dest_address)
                if not data:
                    break
                dest.sendall(data)
            except KeyboardInterrupt:
                if not server.closed:
                    server.close()
                break
            except socket.error:
                if not server.closed:
                    server.close()
                break
    finally:
        source.close()
        dest.close()
        server = None 
Example #10
Source File: pywsgi.py    From PokemonGo-DesktopMap with MIT License 6 votes vote down vote up
def update_environ(self):
        """
        Called before the first request is handled to fill in WSGI environment values.

        This includes getting the correct server name and port.
        """
        address = self.address
        if isinstance(address, tuple):
            if 'SERVER_NAME' not in self.environ:
                try:
                    name = socket.getfqdn(address[0])
                except socket.error:
                    name = str(address[0])
                if PY3 and not isinstance(name, str):
                    name = name.decode('ascii')
                self.environ['SERVER_NAME'] = name
            self.environ.setdefault('SERVER_PORT', str(address[1]))
        else:
            self.environ.setdefault('SERVER_NAME', '')
            self.environ.setdefault('SERVER_PORT', '') 
Example #11
Source File: portforwarder.py    From web_develop with GNU General Public License v3.0 5 votes vote down vote up
def main():
    args = sys.argv[1:]
    if len(args) != 2:
        sys.exit('Usage: %s source-address destination-address' % __file__)
    source = args[0]
    dest = parse_address(args[1])
    server = PortForwarder(source, dest)
    log('Starting port forwarder %s:%s -> %s:%s', *(server.address[:2] + dest))
    gevent.signal(signal.SIGTERM, server.close)
    gevent.signal(signal.SIGINT, server.close)
    server.start()
    gevent.wait() 
Example #12
Source File: test_ha.py    From doge with Apache License 2.0 5 votes vote down vote up
def test_fail_over(self, server, lb):
        ha = FailOverHA(lb.url)
        r = Request("", "sum", 1, 2)
        assert ha.call(r, lb).value == 3 
Example #13
Source File: pywsgi.py    From PhonePi_SampleServer with MIT License 5 votes vote down vote up
def __init__(self, sock, address, server, rfile=None):
        # Deprecation: The rfile kwarg was introduced in 1.0a1 as part
        # of a refactoring. It was never documented or used. It is
        # considered DEPRECATED and may be removed in the future. Its
        # use is not supported.

        self.socket = sock
        self.client_address = address
        self.server = server
        if rfile is None:
            self.rfile = sock.makefile('rb', -1)
        else:
            self.rfile = rfile 
Example #14
Source File: pywsgi.py    From PhonePi_SampleServer with MIT License 5 votes vote down vote up
def log_error(self, msg, *args):
        try:
            message = msg % args
        except Exception: # pylint:disable=broad-except
            traceback.print_exc()
            message = '%r %r' % (msg, args)
        try:
            message = '%s: %s' % (self.socket, message)
        except Exception: # pylint:disable=broad-except
            pass

        try:
            self.server.error_log.write(message + '\n')
        except Exception: # pylint:disable=broad-except
            traceback.print_exc() 
Example #15
Source File: pywsgi.py    From PhonePi_SampleServer with MIT License 5 votes vote down vote up
def log_request(self):
        self.server.log.write(self.format_request() + '\n') 
Example #16
Source File: pywsgi.py    From PhonePi_SampleServer with MIT License 5 votes vote down vote up
def _log_error(self, t, v, tb):
        # TODO: Shouldn't we dump this to wsgi.errors? If we did that now, it would
        # wind up getting logged twice
        if not issubclass(t, GreenletExit):
            context = self.environ
            if not isinstance(context, self.server.secure_environ_class):
                context = self.server.secure_environ_class(context)
            self.server.loop.handle_error(context, t, v, tb) 
Example #17
Source File: ggevent.py    From Flask-P2P with MIT License 5 votes vote down vote up
def log_request(self):
        start = datetime.fromtimestamp(self.time_start)
        finish = datetime.fromtimestamp(self.time_finish)
        response_time = finish - start
        resp_headers = getattr(self, 'response_headers', {})
        resp = GeventResponse(self.status, resp_headers, self.response_length)
        if hasattr(self, 'headers'):
            req_headers = [h.split(":", 1) for h in self.headers.headers]
        else:
            req_headers = []
        self.server.log.access(resp, req_headers, self.environ, response_time) 
Example #18
Source File: dnsproxy.py    From arkc-client with GNU General Public License v2.0 5 votes vote down vote up
def do_read(self):
        try:
            return gevent.server.DatagramServer.do_read(self)
        except socket.error as e:
            if e[0] not in (errno.ECONNABORTED, errno.ECONNRESET, errno.EPIPE):
                raise 
Example #19
Source File: dnsproxy.py    From arkc-client with GNU General Public License v2.0 5 votes vote down vote up
def get_reply_record(self, data):
        request = dnslib.DNSRecord.parse(data)
        qname = str(request.q.qname).lower()
        qtype = request.q.qtype
        dnsservers = self.dns_servers
        if qname.endswith('.in-addr.arpa'):
            ipaddr = '.'.join(reversed(qname[:-13].split('.')))
            record = dnslib.DNSRecord(header=dnslib.DNSHeader(id=request.header.id, qr=1,aa=1,ra=1), a=dnslib.RR(qname, rdata=dnslib.A(ipaddr)))
            return record
        if 'USERDNSDOMAIN' in os.environ:
            user_dnsdomain = '.' + os.environ['USERDNSDOMAIN'].lower()
            if qname.endswith(user_dnsdomain):
                qname = qname[:-len(user_dnsdomain)]
                if '.' not in qname:
                    if not self.dns_intranet_servers:
                        logging.warning('qname=%r is a plain hostname, need intranet dns server!!!', qname)
                        return dnslib.DNSRecord(header=dnslib.DNSHeader(id=request.header.id, rcode=3))
                    qname += user_dnsdomain
                    dnsservers = self.dns_intranet_servers
        try:
            return self.dns_cache.get((qname, qtype))
        except KeyError:
            pass
        try:
            dns_resolve = dnslib_resolve_over_tcp if qname.endswith(self.dns_tcpover) else dnslib_resolve_over_udp
            kwargs = {'blacklist': self.dns_blacklist, 'turstservers': self.dns_trust_servers}
            record = dns_resolve(request, dnsservers, self.dns_timeout, **kwargs)
            ttl = max(x.ttl for x in record.rr) if record.rr else 600
            self.dns_cache.set((qname, qtype), record, ttl * 2)
            return record
        except socket.gaierror as e:
            logging.warning('resolve %r failed: %r', qname, e)
            return dnslib.DNSRecord(header=dnslib.DNSHeader(id=request.header.id, rcode=3)) 
Example #20
Source File: proxy.py    From arkc-client with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        self.fetchservers = kwargs.pop('fetchservers')
        gevent.server.StreamServer.__init__(self, *args, **kwargs)
        self.remote_cache = {} 
Example #21
Source File: main.py    From taserver with GNU Affero General Public License v3.0 5 votes vote down vote up
def main():
    print('Running on Python %s' % sys.version)

    config = configparser.ConfigParser()
    with open(INI_PATH) as f:
        config.read_file(f)

    ports = Ports(int(config['shared']['port_offset']))

    try:
        udp_proxy_proc1 = sp.Popen('udpproxy.exe %d' % ports['gameserver1'])
        udp_proxy_proc2 = sp.Popen('udpproxy.exe %d' % ports['gameserver2'])

    except OSError as e:
        print('Failed to run udpproxy.exe. Run download_udpproxy.py to download it\n'
              'or build it yourself using the Visual Studio solution in the udpproxy\n'
              'subdirectory and place it in the taserver directory.\n',
              file=sys.stderr)
        return

    server_queue = gevent.queue.Queue()
    firewall = Firewall(ports)
    gevent_spawn('firewall.run', firewall.run, server_queue)

    def handle_client(socket, address):
        msg = TcpMessageReader(socket).receive()
        command = json.loads(msg.decode('utf8'))
        server_queue.put(command)

    server = StreamServer(('127.0.0.1', ports['firewall']), handle_client)
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        firewall.remove_all_rules()

    udp_proxy_proc1.terminate()
    udp_proxy_proc2.terminate() 
Example #22
Source File: pywsgi.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def __init__(self, socket, address, server, rfile=None):
        # Deprecation: The rfile kwarg was introduced in 1.0a1 as part
        # of a refactoring. It was never documented or used. It is
        # considered DEPRECATED and may be removed in the future. Its
        # use is not supported.

        self.socket = socket
        self.client_address = address
        self.server = server
        if rfile is None:
            self.rfile = socket.makefile('rb', -1)
        else:
            self.rfile = rfile 
Example #23
Source File: pywsgi.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def log_error(self, msg, *args):
        try:
            message = msg % args
        except Exception:
            traceback.print_exc()
            message = '%r %r' % (msg, args)
        try:
            message = '%s: %s' % (self.socket, message)
        except Exception:
            pass

        try:
            self.server.error_log.write(message + '\n')
        except Exception:
            traceback.print_exc() 
Example #24
Source File: pywsgi.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def log_request(self):
        self.server.log.write(self.format_request() + '\n') 
Example #25
Source File: pywsgi.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def _log_error(self, t, v, tb):
        # TODO: Shouldn't we dump this to wsgi.errors? If we did that now, it would
        # wind up getting logged twice
        if not issubclass(t, GreenletExit):
            self.server.loop.handle_error(self.environ, t, v, tb) 
Example #26
Source File: pywsgi.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def __init__(self, socket, address, server, rfile=None):
        # Deprecation: The rfile kwarg was introduced in 1.0a1 as part
        # of a refactoring. It was never documented or used. It is
        # considered DEPRECATED and may be removed in the future. Its
        # use is not supported.

        self.socket = socket
        self.client_address = address
        self.server = server
        if rfile is None:
            self.rfile = socket.makefile('rb', -1)
        else:
            self.rfile = rfile 
Example #27
Source File: pywsgi.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def log_error(self, msg, *args):
        try:
            message = msg % args
        except Exception:
            traceback.print_exc()
            message = '%r %r' % (msg, args)
        try:
            message = '%s: %s' % (self.socket, message)
        except Exception:
            pass

        try:
            self.server.error_log.write(message + '\n')
        except Exception:
            traceback.print_exc() 
Example #28
Source File: pywsgi.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def log_request(self):
        self.server.log.write(self.format_request() + '\n') 
Example #29
Source File: test_endpoint.py    From doge with Apache License 2.0 5 votes vote down vote up
def test_error(self, server, url):
        ep = EndPoint(url)
        self.ep = ep
        availables = []
        r = Request("", "a")
        for i in range(11):
            ep.call(r)
            availables.append(ep.available)
        assert False in availables
        gevent.sleep(0.2)
        assert ep.available
        r = Request("", "sum", 1, 2)
        assert ep.call(r).value == 3 
Example #30
Source File: ggevent.py    From jbox with MIT License 5 votes vote down vote up
def log_request(self):
        start = datetime.fromtimestamp(self.time_start)
        finish = datetime.fromtimestamp(self.time_finish)
        response_time = finish - start
        resp_headers = getattr(self, 'response_headers', {})
        resp = GeventResponse(self.status, resp_headers, self.response_length)
        if hasattr(self, 'headers'):
            req_headers = [h.split(":", 1) for h in self.headers.headers]
        else:
            req_headers = []
        self.server.log.access(resp, req_headers, self.environ, response_time)