def test_init() -> None: s: GeventSocket = GeventSocket() ctx: ConnectionContext = ConnectionContext(s, ('example.com', 1234, 0, 0)) assert ctx.logger.name == 'example.com:1234' assert ctx.socket is s assert ctx.hostname == 'example.com' assert ctx.port == 1234
def do_read(self): sock = self.socket try: fd, address = sock._accept() except BlockingIOError: # python 2: pylint: disable=undefined-variable if not sock.timeout: return raise sock = GeventSocket(sock.family, sock.type, sock.proto, fileno=fd) # XXX Python issue #7995? return sock, address
def do_read(self): try: client_socket, address = self.socket.accept() except SocketError as err: if err.args[0] == EWOULDBLOCK: return raise sockobj = GeventSocket(_sock=client_socket) if PYPY: # Undo the ref-count bump that the constructor # did. We gave it ownership. client_socket._drop() return sockobj, address
def _tcp_listener(address, backlog=50, reuse_addr=None, family=AF_INET): """A shortcut to create a TCP socket, bind it and put it into listening state.""" sock = GeventSocket(family=family) if reuse_addr is not None: sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, reuse_addr) try: sock.bind(address) except SocketError as ex: strerror = getattr(ex, 'strerror', None) if strerror is not None: ex.strerror = strerror + ': ' + repr(address) raise sock.listen(backlog) sock.setblocking(0) return sock
def do_read(self): sock = self.socket try: fd, address = sock._accept() except BlockingIOError: # python 2: pylint: disable=undefined-variable if not sock.timeout: return raise sock = GeventSocket(sock.family, sock.type, sock.proto, fileno=fd) # XXX Python issue #7995? "if no default timeout is set # and the listening socket had a (non-zero) timeout, force # the new socket in blocking mode to override # platform-specific socket flags inheritance." return sock, address
def _udp_socket(address, backlog=50, reuse_addr=None, family=AF_INET): # backlog argument for compat with tcp_listener # pylint:disable=unused-argument # we want gevent.socket.socket here sock = GeventSocket(family=family, type=SOCK_DGRAM) if reuse_addr is not None: sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, reuse_addr) try: sock.bind(address) except SocketError as ex: strerror = getattr(ex, 'strerror', None) if strerror is not None: ex.strerror = strerror + ': ' + repr(address) raise return sock
def do_read(self): try: client_socket, address = self.socket.accept() except SocketError as err: if err.args[0] == EWOULDBLOCK: return raise # XXX: When would this not be the case? In Python 3 it makes sense # because we're using the low-level _accept method, # but not in Python 2. if not isinstance(client_socket, GeventSocket): # This leads to a leak of the watchers in client_socket sockobj = GeventSocket(_sock=client_socket) if PYPY: client_socket._drop() else: sockobj = client_socket return sockobj, address
def test_hashability() -> None: ctx: ConnectionContext = ConnectionContext(GeventSocket(), ('example.com', 1234, 0, 0)) d: Dict[ConnectionContext, str] = {ctx: 'Testing'} assert d[ctx] == 'Testing'