本文整理汇总了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)