Python gevent.socket() Examples

The following are 30 code examples of gevent.socket(). 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: ggevent.py    From jbox with MIT License 6 votes vote down vote up
def patch(self):
        from gevent import monkey
        monkey.noisy = False

        # if the new version is used make sure to patch subprocess
        if gevent.version_info[0] == 0:
            monkey.patch_all()
        else:
            monkey.patch_all(subprocess=True)

        # monkey patch sendfile to make it none blocking
        patch_sendfile()

        # patch sockets
        sockets = []
        for s in self.sockets:
            if sys.version_info[0] == 3:
                sockets.append(socket(s.FAMILY, _socket.SOCK_STREAM,
                    fileno=s.sock.fileno()))
            else:
                sockets.append(socket(s.FAMILY, _socket.SOCK_STREAM,
                    _sock=s))
        self.sockets = sockets 
Example #2
Source File: peermanager.py    From pydevp2p with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def connect(self, address, remote_pubkey):
        log.debug('connecting', address=address)
        """
        gevent.socket.create_connection(address, timeout=Timeout, source_address=None)
        Connect to address (a 2-tuple (host, port)) and return the socket object.
        Passing the optional timeout parameter will set the timeout
        getdefaulttimeout() is default
        """
        try:
            connection = create_connection(address, timeout=self.connect_timeout)
        except socket.timeout:
            log.info('connection timeout', address=address, timeout=self.connect_timeout)
            self.errors.add(address, 'connection timeout')
            return False
        except socket.error as e:
            log.info('connection error', errno=e.errno, reason=e.strerror)
            self.errors.add(address, 'connection error')
            return False
        self._start_peer(connection, address, remote_pubkey)
        return True 
Example #3
Source File: test_gevent_pool.py    From mars with Apache License 2.0 6 votes vote down vote up
def create_actor_pool(*args, **kwargs):
    import gevent.socket

    address = kwargs.pop('address', None)
    if not address:
        return new_actor_pool(*args, **kwargs)

    if isinstance(address, str):
        port = int(address.rsplit(':', 1)[1])
    else:
        port = DEFAULT_PORT
    it = itertools.count(port)

    auto_port = kwargs.pop('auto_port', True)
    while True:
        try:
            address = '127.0.0.1:{0}'.format(next(it))
            return new_actor_pool(address, *args, **kwargs)
        except gevent.socket.error:
            if auto_port:
                continue
            raise 
Example #4
Source File: cm.py    From steam with MIT License 6 votes vote down vote up
def bootstrap_from_dns(self):
        """
        Fetches CM server list from WebAPI and replaces the current one
        """
        self._LOG.debug("Attempting bootstrap via DNS")

        try:
            answer = socket.getaddrinfo("cm0.steampowered.com",
                                        27017,
                                        socket.AF_INET,
                                        proto=socket.IPPROTO_TCP)
        except Exception as exp:
            self._LOG.error("DNS boostrap failed: %s" % str(exp))
            return False

        servers = list(map(lambda addr: addr[4], answer))

        if servers:
            self.clear()
            self.merge_list(servers)
            return True
        else:
            self._LOG.error("DNS boostrap: cm0.steampowered.com resolved no A records")
            return False 
Example #5
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 #6
Source File: client.py    From AIT-Core with MIT License 6 votes vote down vote up
def __init__(self,
                 zmq_context,
                 zmq_proxy_xsub_url=ait.SERVER_DEFAULT_XSUB_URL,
                 zmq_proxy_xpub_url=ait.SERVER_DEFAULT_XPUB_URL,
                 **kwargs):

        if 'input' in kwargs and type(kwargs['input'][0]) is int:
            super(PortInputClient, self).__init__(zmq_context,
                                                  zmq_proxy_xsub_url,
                                                  zmq_proxy_xpub_url,
                                                  listener=int(kwargs['input'][0]))
        else:
            raise(ValueError('Input must be port in order to create PortInputClient'))

        # open sub socket
        self.sub = gevent.socket.socket(gevent.socket.AF_INET, gevent.socket.SOCK_DGRAM) 
Example #7
Source File: ggevent.py    From Flask-P2P with MIT License 6 votes vote down vote up
def patch(self):
        from gevent import monkey
        monkey.noisy = False

        # if the new version is used make sure to patch subprocess
        if gevent.version_info[0] == 0:
            monkey.patch_all()
        else:
            monkey.patch_all(subprocess=True)

        # monkey patch sendfile to make it none blocking
        patch_sendfile()

        # patch sockets
        sockets = []
        for s in self.sockets:
            sockets.append(socket(s.FAMILY, _socket.SOCK_STREAM,
                _sock=s))
        self.sockets = sockets 
Example #8
Source File: test_bsc.py    From AIT-Core with MIT License 6 votes vote down vote up
def test_mocked_eth_socket_with_rawsocket(self, socket_mock):
        socket_family = getattr(gevent.socket,
                                'AF_PACKET',
                                gevent.socket.AF_INET)

        rawsocket_is_installed = True if bsc.RAW_SOCKET_FD else False
        if not rawsocket_is_installed:
            rawsocket_fd = 'fake_rawsocket_fd'
            bsc.RAW_SOCKET_FD = rawsocket_fd
        else:
            rawsocket_fd = bsc.RAW_SOCKET_FD

        handler = {'name':'name', 'log_dir':'/tmp'}
        sl = bsc.SocketStreamCapturer([handler], ['eho0', 0], 'ethernet')
        # We need to test a different load if the rawsocket package is used
        socket_mock.fromfd.assert_called_with(rawsocket_fd,
                                              socket_family,
                                              gevent.socket.SOCK_RAW,
                                              socket.htons(bsc.ETH_PROTOCOL))
        assert sl.conn_type == 'ethernet'

        if not rawsocket_is_installed:
            bsc.RAW_SOCKET_FD = None 
Example #9
Source File: net_gevent.py    From rethinkdb-python with Apache License 2.0 6 votes vote down vote up
def run_query(self, query, noreply):
        self._write_mutex.acquire()

        try:
            self._socket.sendall(query.serialize(self._parent._get_json_encoder(query)))
        finally:
            self._write_mutex.release()

        if noreply:
            return None

        async_res = AsyncResult()
        self._user_queries[query.token] = (query, async_res)
        return async_res.get()

    # The _reader coroutine runs in its own coroutine in parallel, reading responses
    # off of the socket and forwarding them to the appropriate AsyncResult or Cursor.
    # This is shut down as a consequence of closing the stream, or an error in the
    # socket/protocol from the server.  Unexpected errors in this coroutine will
    # close the ConnectionInstance and be passed to any open AsyncResult or Cursors. 
Example #10
Source File: test_bsc.py    From AIT-Core with MIT License 6 votes vote down vote up
def test_mocked_eth_socket(self, socket_mock):
        socket_family = getattr(gevent.socket,
                                'AF_PACKET',
                                gevent.socket.AF_INET)
        proto = bsc.ETH_PROTOCOL
        handler = {'name':'name', 'log_dir':'/tmp'}
        bsc.RAW_SOCKET_FD = 'foobar'
        sl = bsc.SocketStreamCapturer([handler], ['eho0', 0], 'ethernet')
        # We need to test a different load if the rawsocket package is used
        if not bsc.RAW_SOCKET_FD:
            socket_mock.socket.assert_called_with(socket_family,
                                                  gevent.socket.SOCK_RAW,
                                                  socket.htons(proto))
        else:
            socket_mock.fromfd.assert_called_with(bsc.RAW_SOCKET_FD,
                                                  socket_family,
                                                  gevent.socket.SOCK_RAW,
                                                  socket.htons(proto))
        assert sl.conn_type == 'ethernet'
        bsc.RAW_SOCKET_FD = None 
Example #11
Source File: peer.py    From pydevp2p with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _run_ingress_message(self):
        gevent.spawn(self._run_decoded_packets)
        gevent.spawn(self._run_egress_message)

        while not self.is_stopped:
            self.safe_to_read.wait()
            gevent.socket.wait_read(self.connection.fileno())
            try:
                imsg = self.connection.recv(4096)
            except gevent.socket.error as e:
                log.info('read error', errno=e.errno, reason=e.strerror, peer=self)
                self.report_error('network error %s' % e.strerror)
                if e.errno in(50, 54, 60, 65):
                    # (Network down, Connection reset by peer, timeout, nor route to host)
                    self.stop()
                else:
                    raise e
                    break
            if imsg:
                try:
                    self.mux.add_message(imsg)
                except rlpxcipher.RLPxSessionError as e:
                    log.debug('rlpx session error', peer=self, error=e)
                    self.report_error('rlpx session error')
                    self.stop()
                except multiplexer.MultiplexerError as e:
                    log.debug('multiplexer error', peer=self, error=e)
                    self.report_error('multiplexer error')
                    self.stop()
            else:
                log.debug('no data on socket', peer=self)
                self.report_error('no data on socket')
                self.stop() 
Example #12
Source File: acehttp.py    From HTTPAceProxy with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, socket, client_address):
        try: BaseHTTPRequestHandler.__init__(self, socket, client_address, self)
        except: pass 
Example #13
Source File: peermanager.py    From pydevp2p with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _bootstrap(self, bootstrap_nodes=[]):
        for uri in bootstrap_nodes:
            ip, port, pubkey = utils.host_port_pubkey_from_uri(uri)
            log.info('connecting bootstrap server', uri=uri)
            try:
                self.connect((ip, port), pubkey)
            except socket.error:
                log.warn('connecting bootstrap server failed') 
Example #14
Source File: discovery.py    From pydevp2p with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def send(self, address, message):
        assert isinstance(address, Address)
        log.debug('sending', size=len(message), to=address)
        try:
            self.server.sendto(message, (address.ip, address.port))
        except gevent.socket.error as e:
            log.critical('udp write error', errno=e.errno, reason=e.strerror)
            self.app.stop() 
Example #15
Source File: net_gevent.py    From rethinkdb-python with Apache License 2.0 5 votes vote down vote up
def close(self):
        if self._socket is not None:
            try:
                self._socket.shutdown(socket.SHUT_RDWR)
                self._socket.close()
            except Exception as ex:
                default_logger.error(ex.message)
            finally:
                self._socket = None 
Example #16
Source File: common.py    From ahjs5s with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, config):
        dict.__init__(self, config)
        if 'server_sockfamily' in self:
            self['server_sockfamily'] = getattr(socket, self['server_sockfamily'])
        else:
            self['server_sockfamily'] = socket.AF_INET
        if not 'server_addr' in self or not self['server_addr']:
            if self['server_sockfamily'] == socket.AF_INET:
                self['server_addr'] = '0.0.0.0'
            elif self['server_sockfamily'] == socket.AF_INET6:
                self['server_addr'] = '::'
        if not 'backlog' in self:
            self['backlog'] = 50 
Example #17
Source File: common.py    From ahjs5s with GNU General Public License v2.0 5 votes vote down vote up
def start(self):
        try:
            sock = socket.socket(self.config['server_sockfamily'])
            sock.bind((self.config['server_addr'], self.config['server_port']))
            sock.listen(self.config['backlog'])
        except Exception as e:
            self.config['logger'].error('Exception when starting: %s', str(e))
            return
        while True:
            try:
                self.start_peer(*sock.accept())
            except Exception as e:
                self.config['logger'].info('Exception when accept socks client: %s', str(e)) 
Example #18
Source File: ggevent.py    From Flask-P2P with MIT License 5 votes vote down vote up
def get_environ(self):
        env = super(PyWSGIHandler, self).get_environ()
        env['gunicorn.sock'] = self.socket
        env['RAW_URI'] = self.path
        return env 
Example #19
Source File: portforwarder.py    From web_develop with GNU General Public License v3.0 5 votes vote down vote up
def close(self):
        if self.closed:
            sys.exit('Multiple exit signals received - aborting.')
        else:
            log('Closing listener socket')
            StreamServer.close(self) 
Example #20
Source File: subscription_manager.py    From graphql-python-subscriptions with MIT License 5 votes vote down vote up
def __init__(self, host='localhost', port=6379, *args, **kwargs):
        redis.connection.socket = gevent.socket
        self.redis = redis.StrictRedis(host, port, *args, **kwargs)
        self.pubsub = self.redis.pubsub()
        self.subscriptions = {}
        self.sub_id_counter = 0
        self.greenlet = None 
Example #21
Source File: dbpool.py    From sqlchain with MIT License 5 votes vote down vote up
def apply(self,function,*args):
        logging.info(args)

        self.state.function = function
        self.state.args = args
        gevent.socket.wait_write(self.pipe[0].fileno())
        self.pipe[0].send('\0')
        gevent.socket.wait_read(self.pipe[0].fileno())
        self.pipe[0].recv(1)
        if self.state.status != 0:
            raise self.state.error
        return self.state.ret 
Example #22
Source File: peer.py    From pydevp2p with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def send(self, data):
        if not data:
            return
        self.safe_to_read.clear()  # make sure we don't accept any data until message is sent
        try:
            self.connection.sendall(data)  # check if gevent chunkes and switches contexts
        except gevent.socket.error as e:
            log.info('write error', errno=e.errno, reason=e.strerror)
            self.report_error('write error %r' % e.strerror)
            self.stop()
        except gevent.socket.timeout:
            log.info('write timeout')
            self.report_error('write timeout')
            self.stop()
        self.safe_to_read.set() 
Example #23
Source File: peer.py    From pydevp2p with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __repr__(self):
        try:
            pn = self.connection.getpeername()
        except gevent.socket.error:
            pn = ('not ready',)
        try:
            cv = '/'.join(self.remote_client_version.split('/')[:2])
        except:
            cv = self.remote_client_version
        return '<Peer%r %s>' % (pn, cv)
        # return '<Peer%r>' % repr(pn) 
Example #24
Source File: acehttp.py    From HTTPAceProxy with GNU General Public License v3.0 5 votes vote down vote up
def get_ip_address():
    # connecting to a UDP address doesn't send packets
    try: return [(s.connect(('1.1.1.1', 0)), s.getsockname()[0], s.close()) for s in [socket(AF_INET, SOCK_DGRAM)]][0][1]
    except:
       logger.error('Network is unreachable')
       sys.exit() 
Example #25
Source File: test_gipc.py    From gipc with MIT License 5 votes vote down vote up
def test_getaddrinfo_mp(self):
        """This test would make gevent's hub threadpool kill upon hub
        destruction in child block forever. Gipc resolves this by killing
        threadpool even harder.
        """
        import gevent.socket as socket
        socket.getaddrinfo("localhost", 21)
        p = start_process(target=complchild_test_getaddrinfo_mp)
        p.join(timeout=1)
        assert p.exitcode == 0 
Example #26
Source File: test_bsc.py    From AIT-Core with MIT License 5 votes vote down vote up
def test_capture_with_data_manip(self, socket_mock, pcap_open_mock, pcap_stream_mock):
        transform_mock = mock.Mock(side_effect=['transformed data'])
        handler = {
            'name': 'name',
            'log_dir': '/tmp',
            'pre_write_transforms': [transform_mock]
        }
        sl = bsc.SocketStreamCapturer([handler], ['', 9000], 'udp')
        logger = sl.capture_handlers[0]['logger']
        sl.socket.recv.return_value = 'udp_data'
        sl.capture_packet()

        assert transform_mock.called
        logger.write.assert_called_with('transformed data') 
Example #27
Source File: test_bsc.py    From AIT-Core with MIT License 5 votes vote down vote up
def test_packet_log_mutliple_handlers(self, socket_mock, pcap_open_mock, pcap_stream_mock):
        h1 = {'name':'h1', 'log_dir':'/tmp'}
        h2 = {'name':'h2', 'log_dir':'/tmp'}
        sl = bsc.SocketStreamCapturer([h1, h2], ['', 9000], 'udp')

        sl.capture_handlers[0]['logger'] = mock.MagicMock()
        sl.capture_handlers[1]['logger'] = mock.MagicMock()
        logger1 = sl.capture_handlers[0]['logger']
        logger2 = sl.capture_handlers[1]['logger']
        sl.socket.recv.return_value = 'udp_data'
        sl.capture_packet()

        assert logger1.write.call_count == 1
        assert logger2.write.call_count == 1 
Example #28
Source File: test_bsc.py    From AIT-Core with MIT License 5 votes vote down vote up
def test_mocked_udp_socket(self, socket_mock):
        handler = {'name':'name', 'log_dir':'/tmp'}
        sl = bsc.SocketStreamCapturer([handler], ['', 9000], 'udp')
        socket_mock.assert_called_with(gevent.socket.AF_INET,
                                       gevent.socket.SOCK_DGRAM)
        assert sl.conn_type == 'udp' 
Example #29
Source File: bsc.py    From AIT-Core with MIT License 5 votes vote down vote up
def _add_logger_by_name(self, name):
        ''' Handles POST requests for adding a new logger.

        Expects logger configuration to be passed in the request's query string.
        The logger name is included in the URL and the address components and
        connection type should be included as well. The loc attribute is
        defaulted to "localhost" when making the socket connection if not
        defined.

        loc = IP / interface
        port = port / protocol
        conn_type = udp or ethernet

        Raises:
            ValueError:
                if the port or connection type are not supplied.
        '''
        data = dict(request.forms)
        loc = data.pop('loc', '')
        port = data.pop('port', None)
        conn_type = data.pop('conn_type', None)

        if not port or not conn_type:
            e = 'Port and/or conn_type not set'
            raise ValueError(e)
        address = [loc, int(port)]

        if 'rotate_log' in data:
            data['rotate_log'] = True if data == 'true' else False

        if 'rotate_log_delta' in data:
            data['rotate_log_delta'] = int(data['rotate_log_delta'])

        self._logger_manager.add_logger(name, address, conn_type, **data) 
Example #30
Source File: bsc.py    From AIT-Core with MIT License 5 votes vote down vote up
def socket_monitor_loop(self):
        ''' Monitor the socket and log captured data. '''
        try:
            while True:
                gevent.socket.wait_read(self.socket.fileno())

                self._handle_log_rotations()
                self.capture_packet()
        finally:
            self.clean_up()