本文整理匯總了Python中gevent.socket.timeout方法的典型用法代碼示例。如果您正苦於以下問題:Python socket.timeout方法的具體用法?Python socket.timeout怎麽用?Python socket.timeout使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類gevent.socket
的用法示例。
在下文中一共展示了socket.timeout方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: connect
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import timeout [as 別名]
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
示例2: sendall
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import timeout [as 別名]
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
示例3: sendall
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import timeout [as 別名]
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)
示例4: sendall
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import timeout [as 別名]
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)
示例5: __forwardData
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import timeout [as 別名]
def __forwardData(self,s,d,o,data_timeout=5*60):
# TODO: 這裏沒有處理單方向關閉連接的情況。
try:
while True:
try:
data=s.recv(65536)
#logging.debug(u'len(data)=%s'%len(data))
if not data:
break
o['forward_data_time'] = int(time.time()*1000)
except _socket.timeout as e:
if o['forward_data_time'] + data_timeout > int(time.time()*1000):
# 解決下慢速下載長時間無上傳造成讀超時斷開連接的問題
continue
raise
d.sendall(data)
except _socket.timeout as e:
logging.debug(u'連接長時間無數據,關閉。')
except _socket.error as e :
if e.errno == 9:
# 另一個協程關閉了鏈接。
pass
elif e.errno == 10053:
# 遠端關閉了連接
logging.debug(u'遠端關閉了連接。')
pass
elif e.errno == 10054:
# 遠端重置了連接
#TODO: 部分網站直連可以成功,但是中途會重置鏈接。但是 multipath 隻在建立連接部分做測試,並沒有處理建立連接後被重置的情況。。。
logging.debug(u'遠端重置了連接。')
pass
else:
logging.exception(u'DirectProxy.__forwardData')
finally:
# 這裏 和 socks5 Handle 會重複關閉
logging.debug(u'DirectProxy.__forwardData finally')
gevent.sleep(5)
s.close()
d.close()
示例6: __init__
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import timeout [as 別名]
def __init__(self, timeout=60):
self.timeout = timeout
示例7: produce_danmaku
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import timeout [as 別名]
def produce_danmaku(sock, danmaku_queue, is_health=True):
"""Produce danmakus.
:param sock: the socket object.
:param danmaku_queue: the queue to recieve danmaku.
:param is_health: the status of connection
"""
start = time.time()
while True:
end = time.time()
if end - start > HEARTBEAT_KEEP_TIME:
start = time.time()
heartbeat.switch(sock, danmaku_queue, is_health)
try:
data = sock.recv(10240)
if not data:
break
except socket.timeout:
if not is_health:
print "連接超時,準備重連服務器。。。"
break
except socket.error:
break
status = process_recieve_data(danmaku_queue, data)
if status:
consume_danmaku.switch(sock, danmaku_queue, is_health)
示例8: heartbeat
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import timeout [as 別名]
def heartbeat(sock, danmaku_queue, is_health):
"""Keep the connection alive.
:param sock: the socket object.
:param danmaku_queue: the queue to recieve danmaku.
:param is_health: the status of connection
"""
while True:
try:
send_data = send_socket_data(sock, 16, 16, 1, 2)
except socket.timeout:
is_health = False
produce_danmaku.switch(sock, danmaku_queue, is_health)
示例9: test_no_logging_until_many_transient_error
# 需要導入模塊: from gevent import socket [as 別名]
# 或者: from gevent.socket import timeout [as 別名]
def test_no_logging_until_many_transient_error():
transient = [
socket.timeout,
socket.error,
_mysql_exceptions.OperationalError(
"(_mysql_exceptions.OperationalError) (1213, 'Deadlock "
"found when trying to get lock; try restarting transaction')"),
_mysql_exceptions.OperationalError(
"(_mysql_exceptions.OperationalError) Lost connection to MySQL "
"server during query"),
_mysql_exceptions.OperationalError(
"(_mysql_exceptions.OperationalError) MySQL server has gone away."),
_mysql_exceptions.OperationalError(
"(_mysql_exceptions.OperationalError) Can't connect to MySQL "
"server on 127.0.0.1"),
_mysql_exceptions.OperationalError(
"(_mysql_exceptions.OperationalError) Max connect timeout reached "
"while reaching hostgroup 71"),
StatementError(
message="?", statement="SELECT *", params={},
orig=_mysql_exceptions.OperationalError(
"(_mysql_exceptions.OperationalError) MySQL server has gone away.")),
]
for transient_exc in transient:
logger = MockLogger()
failing_function = FailingFunction(transient_exc, max_executions=2)
retry_with_logging(failing_function, logger=logger)
assert logger.call_count == 0, '{} should not be logged'.format(transient_exc)
assert failing_function.call_count == 2
failing_function = FailingFunction(socket.error, max_executions=21)
retry_with_logging(failing_function, logger=logger)
assert logger.call_count == 1
assert failing_function.call_count == 21
failing_function = FailingFunction(socket.error, max_executions=2)