问题描述
看起来asyncio是要使用的模块。我会离开这个问题,因为它看起来不像有一个方法来捕捉具体的错误与asynchat。
Looks like asyncio is the module to use. I'll leave this question up anyway, because it doesn't look like there is a way to catch specific errors with asynchat.
class mysocket(asynchat.async_chat):
terminator = b'\n'
def __init__(self,sock=None):
asynchat.async_chat.__init__(self,sock)
self.create_socket()
# Try always succeeds with self.connect
try:
self.connect(('badhost',6667))
print('command always successful')
except:
print('This never gets printed')
如何从引发未捕获异常的self.connect()方法中捕获错误。
How do I catch errors from the self.connect() method that causes an uncaught exception.
错误:uncaptured python exception,closing channel< main .mysocket badhost:6667 at 0x7f0a03e66a58>(:[Errno 111] Connection refused [/usr/lib/python3.4/asyncore.py|read|83] [/usr/lib/python3.4/asyncore。 py | handle_read_event | 439] [/ usr / l ib / python3.4 / asyncore.py | handle_connect_event | 447])
error: uncaptured python exception, closing channel <main.mysocket badhost:6667 at 0x7f0a03e66a58> (:[Errno 111] Connection refused [/usr/lib/python3.4/asyncore.py|read|83] [/usr/lib/python3.4/asyncore.py|handle_read_event|439] [/usr/lib/python3.4/asyncore.py|handle_connect_event|447])
所有要尝试的都是覆盖handle_connect_event()方法,并将asyncore.handle_connect_event(self )。
All that is left to try is overwrite the handle_connect_event() method and put asyncore.handle_connect_event(self). I would like to get a professional answer to this dilemma.
推荐答案
尝试覆盖默认的handle_error方法:
Try to override default handle_error method:
def handle_error(self):
t, v, tb = sys.exc_info()
if t == socket.error:
# do smth with it
print("Connect error")
这篇关于Python3从self.connect(('badhost',6667))捕获错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!