我们从Python开源项目中,提取了以下27个代码示例,用于说明如何使用gevent.socket.timeout()。
def send(self, data, flags=0, timeout=timeout_default): if timeout is timeout_default: timeout = self.timeout while True: try: return self._sock.send(data, flags) except SSL.WantWriteError, ex: if self.timeout == 0.0: raise timeout(str(ex)) else: sys.exc_clear() wait_write(self.fileno(), timeout=timeout) except SSL.WantReadError, ex: if self.timeout == 0.0: raise timeout(str(ex)) else: sys.exc_clear() wait_read(self.fileno(), timeout=timeout) except SSL.SysCallError, ex: if ex[0] == -1 and data == "": # errors when writing empty strings are expected and can be ignored return 0 raise sslerror(SysCallError_code_mapping.get(ex.args[0], ex.args[0]), ex.args[1]) except SSL.Error, ex: raise sslerror(str(ex))
def recv(self, buflen): pending = self._sock.pending() if pending: return self._sock.recv(min(pending, buflen)) while True: try: return self._sock.recv(buflen) except SSL.WantReadError, ex: if self.timeout == 0.0: raise timeout(str(ex)) else: sys.exc_clear() wait_read(self.fileno(), timeout=self.timeout) except SSL.WantWriteError, ex: if self.timeout == 0.0: raise timeout(str(ex)) else: sys.exc_clear() wait_read(self.fileno(), timeout=self.timeout) except SSL.ZeroReturnError: return '' except SSL.SysCallError, ex: raise sslerror(SysCallError_code_mapping.get(ex.args[0], ex.args[0]), ex.args[1]) except SSL.Error, ex: raise sslerror(str(ex))
def ssl(sock, keyfile=None, certfile=None): context = SSL.Context(SSL.SSLv23_METHOD) if certfile is not None: context.use_certificate_file(certfile) if keyfile is not None: context.use_privatekey_file(keyfile) context.set_verify(SSL.VERIFY_NONE, lambda *x: True) timeout = sock.gettimeout() try: sock = sock._sock except AttributeError: pass connection = SSL.Connection(context, sock) ssl_sock = SSLObject(connection) ssl_sock.settimeout(timeout) try: sock.getpeername() except Exception: # no, no connection yet pass else: # yes, do the handshake ssl_sock.do_handshake() return ssl_sock
def check(self, config): self.data = super(ZookeeperCollector, self).check(config) try: # command args cx_args = (config['host'], config['zkcli_port'], config['timeout']) # Read metrics from the `stat` output. stat_out = self._send_command('stat', *cx_args) self.data = self.parse_stat(stat_out, self.data) # Read status from the `ruok` output. ruok_out = self._send_command('ruok', *cx_args) ruok_out.seek(0) ruok = ruok_out.readline().strip() if ruok == 'imok': self.data['server.status'] = 0 else: self.data['server.status'] = 1 except Exception, e: print e.message finally: return self.data
def sendall(self, data, flags=0): self._checkClosed() self.__check_flags('sendall', flags) try: socket.sendall(self, data) except _socket_timeout as ex: if self.timeout == 0.0: # Python 2 simply *hangs* in this case, which is bad, but # Python 3 raises SSLWantWriteError. We do the same. raise SSLWantWriteError("The operation did not complete (write)") # Convert the socket.timeout back to the sslerror raise SSLError(*ex.args)
def sendall(self, data, flags=0): self._checkClosed() if self._sslobj: if flags != 0: raise ValueError( "non-zero flags not allowed in calls to sendall() on %s" % self.__class__) try: return socket.sendall(self, data, flags) except _socket_timeout: if self.timeout == 0.0: # Raised by the stdlib on non-blocking sockets raise SSLWantWriteError("The operation did not complete (write)") raise
def sendall(self, data, flags=0): try: socket.sendall(self, data) except _socket_timeout as ex: if self.timeout == 0.0: # Python 2 simply *hangs* in this case, which is bad, but # Python 3 raises SSLWantWriteError. We do the same. raise SSLError(SSL_ERROR_WANT_WRITE) # Convert the socket.timeout back to the sslerror raise SSLError(*ex.args)
def _send_command(self, command, host, port, timeout): sock = socket.socket() sock.settimeout(timeout) buf = StringIO.StringIO() chunk_size = 4096 # try-finally and try-except to stay compatible with python 2.4 try: try: # Connect to the zk client port and send the stat command sock.connect((host, port)) sock.sendall(command) # Read the response into a StringIO buffer chunk = sock.recv(chunk_size) buf.write(chunk) num_reads = 1 max_reads = 100 while chunk: if num_reads > max_reads: # Safeguard against an infinite loop raise Exception("Read %s bytes before exceeding max reads of %s. " % (buf.tell(), max_reads)) chunk = sock.recv(chunk_size) buf.write(chunk) num_reads += 1 except (socket.timeout, socket.error): raise Exception("the execution of command is timeout") finally: sock.close() return buf
def make_request(self, method, params): request = self.encode_rpc_request(method, params) with get_ipc_socket(self.ipc_path) as sock: sock.sendall(request) response_raw = b"" with gevent.Timeout(10): while True: try: response_raw += sock.recv(4096) except socket.timeout: gevent.sleep(0) continue if response_raw == b"": gevent.sleep(0) else: try: json.loads(force_text(response_raw)) except JSONDecodeError: gevent.sleep(0) continue else: break return response_raw