我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用gevent.socket.socket()。
def __init__(self, listener, handle=None, backlog=None, spawn='default', **ssl_args): BaseServer.__init__(self, listener, handle=handle, spawn=spawn) try: if ssl_args: ssl_args.setdefault('server_side', True) from gevent.ssl import wrap_socket self.wrap_socket = wrap_socket self.ssl_args = ssl_args else: self.ssl_args = None if backlog is not None: if hasattr(self, 'socket'): raise TypeError('backlog must be None when a socket instance is passed') self.backlog = backlog except: self.close() raise
def _tcp_listener(address, backlog=50, reuse_addr=None, family=_socket.AF_INET): """A shortcut to create a TCP socket, bind it and put it into listening state.""" sock = socket(family=family) if reuse_addr is not None: sock.setsockopt(_socket.SOL_SOCKET, _socket.SO_REUSEADDR, reuse_addr) try: sock.bind(address) except _socket.error: ex = sys.exc_info()[1] 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 patch_all(socket=True, dns=True, time=True, select=True, thread=True, os=True, ssl=True, httplib=False, aggressive=True): """Do all of the default monkey patching (calls every other function in this module.""" # order is important if os: patch_os() if time: patch_time() if thread: patch_thread() if socket: patch_socket(dns=dns, aggressive=aggressive) if select: patch_select(aggressive=aggressive) if ssl: patch_ssl() if httplib: patch_httplib()
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
def __init__(self, listener, handle=None, backlog=None, spawn='default', **ssl_args): BaseServer.__init__(self, listener, handle=handle, spawn=spawn) try: if ssl_args: ssl_args.setdefault('server_side', True) if 'ssl_context' in ssl_args: ssl_context = ssl_args.pop('ssl_context') self.wrap_socket = ssl_context.wrap_socket self.ssl_args = ssl_args else: from gevent.ssl import wrap_socket self.wrap_socket = wrap_socket self.ssl_args = ssl_args else: self.ssl_args = None if backlog is not None: if hasattr(self, 'socket'): raise TypeError('backlog must be None when a socket instance is passed') self.backlog = backlog except: self.close() raise
def _udp_socket(address, backlog=50, reuse_addr=None, family=_socket.AF_INET): # backlog argument for compat with tcp_listener # pylint:disable=unused-argument # we want gevent.socket.socket here sock = socket(family=family, type=_socket.SOCK_DGRAM) if reuse_addr is not None: sock.setsockopt(_socket.SOL_SOCKET, _socket.SO_REUSEADDR, reuse_addr) try: sock.bind(address) except _socket.error as ex: strerror = getattr(ex, 'strerror', None) if strerror is not None: ex.strerror = strerror + ': ' + repr(address) raise return sock
def receive_events(self): buf = '' while self._run: try: data = self.sock_file.readline() except Exception: self._run = False self.connected = False self.sock.close() # logging.exception("Error reading from socket.") break if not data: if self.connected: logging.error("Error receiving data, is FreeSWITCH running?") self.connected = False break # Empty line if data == self._EOL: event = ESLEvent(buf) buf = '' self.handle_event(event) continue buf += data
def getLocalIp(self): import socket, fcntl, struct f = open('/proc/net/dev') if_list = [] while True: line = f.readline() if line: dev_info = line.split(":") if len(dev_info) < 2: continue; if_list.append(dev_info[0].strip()) else: break ip_list = [] s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) for eth in if_list: try: inet = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', eth[:15])) ip = socket.inet_ntoa(inet[20:24]) except Exception, e: continue ip_list.append(ip) return ip_list
def can_read(self, timeout=0.0): """ Checks if there is data that can be read from the socket (if open). Returns True if there is data and False if not. It returns None if something very bad happens such as a dead connection (bad file descriptor), etc """ # rs = Read Sockets # ws = Write Sockets # es = Error Sockets if self.socket is not None: try: rs, _, es = select([self.socket], [], [], timeout) except (SelectError, socket.error), e: if e[0] == errno.EBADF: # Bad File Descriptor... hmm self.close() return None if len(es) > 0: # Bad File Descriptor self.close() return None return len(rs) > 0 # no socket or no connection return None
def can_write(self, timeout=0): """ Checks if there is data that can be written to the socket (if open). Returns True if writing is possible and False if not. It returns None if something very bad happens such as a dead connection (bad file descriptor), etc """ # rs = Read Sockets # ws = Write Sockets # es = Error Sockets if self.socket is not None: try: _, ws, es = select([], [self.socket], [], timeout) except (SelectError, socket.error), e: if e[0] == errno.EBADF: # Bad File Descriptor... hmm self.close() return None if len(es) > 0: # Bad File Descriptor self.close() return None return len(ws) > 0 # no socket or no connection return None
def local_connection_info(self): """ Returns a tuple of current address of 'this' server if listening, then it is the listing server. If performing a remote connection, then it is the address that was made in a bind() call If no connection has been established, the connection returns None. """ if self.socket is None: return None return (self._local_addr, self._local_port)
def remote_connection_info(self): """ Returns a tuple of current address of 'this' server if listening, then it is the listing server. If performing a remote connection, then it is the address that was made in a bind() call If no connection has been established, the connection returns None. """ if self.socket is None: return None return (self._remote_addr, self._remote_port)
def is_eventlet_monkey_patched(): if 'eventlet.patcher' not in sys.modules: return False import eventlet.patcher return eventlet.patcher.is_monkey_patched('socket')
def is_gevent_monkey_patched(): if 'gevent.monkey' not in sys.modules: return False import gevent.socket return socket.socket is gevent.socket.socket