Python 使用asyncio与其他对等方连接时如何处理ConnectionRefusedError
我使用Python 使用asyncio与其他对等方连接时如何处理ConnectionRefusedError,python,python-3.x,p2p,bittorrent,python-asyncio,Python,Python 3.x,P2p,Bittorrent,Python Asyncio,我使用asyncio与我正在使用的bittorrent客户端中的其他对等方进行连接。当某些对等机无法连接时,程序崩溃,出现ConnectionRefusedError和TimeoutError异常。有些同龄人无法连接是正常的还是我的代码有问题。如果这是正常的,我应该如何处理异常?我试着在循环中放置try-except。创建连接(),但没有任何效果 这是我的密码: class Torrent(): def __init__(self, torrent_file, loop):
asyncio
与我正在使用的bittorrent客户端中的其他对等方进行连接。当某些对等机无法连接时,程序崩溃,出现ConnectionRefusedError
和TimeoutError
异常。有些同龄人无法连接是正常的还是我的代码有问题。如果这是正常的,我应该如何处理异常?我试着在循环中放置try-except
。创建连接()
,但没有任何效果
这是我的密码:
class Torrent():
def __init__(self, torrent_file, loop):
self.torrent = Torrent(torrent_file)
self.peers = self.get_peers()
self.loop = loop
...
def connect_to_peers(self):
tasks = []
for peer in self.peers:
try:
# returns a coroutine
connection = self.loop.create_connection(PeerProtocol, peer['host'], peer['port'])
tasks.append(asyncio.Task(connection))
except ConnectionRefusedError:
print('caught')
except TimeoutError:
print('timeout error')
return tasks
def main():
loop = asyncio.get_event_loop()
filename = 'street-fighter.torrent'
client = TorrentClient(filename, loop)
tasks = client.connect_to_peers()
try:
loop.run_until_complete(asyncio.wait(tasks))
except KeyboardInterrupt:
pass
class PeerProtocol(asyncio.Protocol):
def connection_made(self, transport):
host, port = transport.get_extra_info('peername')
print('connected with {}:{}'.format(host, port))
def connection_lost(self, exc):
print('disconnected...')
print('exc: {}'.format(exc))
以下是输出:
connected with 80.94.76.7:14122
connected with 174.110.236.233:45308
connected with 78.177.119.170:27311
connected with 95.15.59.242:21426
disconnected...
exc: [Errno 54] Connection reset by peer
disconnected...
exc: [Errno 54] Connection reset by peer
disconnected...
exc: None
disconnected...
exc: None
Task exception was never retrieved
future: <Task finished coro=<BaseEventLoop.create_connection() done, defined at /Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py:548> exception=TimeoutError(60, "Connect call failed ('93.34.49.17', 13311)")>
Traceback (most recent call last):
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py", line 241, in _step
result = coro.throw(exc)
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py", line 645, in create_connection
raise exceptions[0]
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py", line 632, in create_connection
yield from self.sock_connect(sock, address)
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 358, in __iter__
yield self # This tells Task to wait for completion.
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py", line 290, in _wakeup
future.result()
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/selector_events.py", line 436, in _sock_connect_cb
raise OSError(err, 'Connect call failed %s' % (address,))
TimeoutError: [Errno 60] Connect call failed ('93.34.49.17', 13311)
Task exception was never retrieved
future: <Task finished coro=<BaseEventLoop.create_connection() done, defined at /Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py:548> exception=TimeoutError(60, "Connect call failed ('197.29.6.31', 50735)")>
Traceback (most recent call last):
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py", line 241, in _step
result = coro.throw(exc)
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py", line 645, in create_connection
raise exceptions[0]
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py", line 632, in create_connection
yield from self.sock_connect(sock, address)
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 358, in __iter__
yield self # This tells Task to wait for completion.
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py", line 290, in _wakeup
future.result()
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/selector_events.py", line 436, in _sock_connect_cb
raise OSError(err, 'Connect call failed %s' % (address,))
TimeoutError: [Errno 60] Connect call failed ('197.29.6.31', 50735)
Task exception was never retrieved
future: <Task finished coro=<BaseEventLoop.create_connection() done, defined at /Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py:548> exception=TimeoutError(60, "Connect call failed ('195.174.165.47', 61567)")>
Traceback (most recent call last):
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py", line 241, in _step
result = coro.throw(exc)
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py", line 645, in create_connection
raise exceptions[0]
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py", line 632, in create_connection
yield from self.sock_connect(sock, address)
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 358, in __iter__
yield self # This tells Task to wait for completion.
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py", line 290, in _wakeup
future.result()
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/selector_events.py", line 436, in _sock_connect_cb
raise OSError(err, 'Connect call failed %s' % (address,))
TimeoutError: [Errno 60] Connect call failed ('195.174.165.47', 61567)
Task exception was never retrieved
future: <Task finished coro=<BaseEventLoop.create_connection() done, defined at /Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py:548> exception=ConnectionRefusedError(61, "Connect call failed ('69.122.194.81', 6881)")>
Traceback (most recent call last):
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py", line 241, in _step
result = coro.throw(exc)
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py", line 645, in create_connection
raise exceptions[0]
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py", line 632, in create_connection
yield from self.sock_connect(sock, address)
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 358, in __iter__
yield self # This tells Task to wait for completion.
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py", line 290, in _wakeup
future.result()
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/selector_events.py", line 436, in _sock_connect_cb
raise OSError(err, 'Connect call failed %s' % (address,))
ConnectionRefusedError: [Errno 61] Connect call failed ('69.122.194.81', 6881)
Task exception was never retrieved
future: <Task finished coro=<BaseEventLoop.create_connection() done, defined at /Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py:548> exception=TimeoutError(60, "Connect call failed ('41.210.123.12', 48319)")>
Traceback (most recent call last):
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py", line 241, in _step
result = coro.throw(exc)
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py", line 645, in create_connection
raise exceptions[0]
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py", line 632, in create_connection
yield from self.sock_connect(sock, address)
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 358, in __iter__
yield self # This tells Task to wait for completion.
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py", line 290, in _wakeup
future.result()
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/selector_events.py", line 436, in _sock_connect_cb
raise OSError(err, 'Connect call failed %s' % (address,))
TimeoutError: [Errno 60] Connect call failed ('41.210.123.12', 48319)
Task exception was never retrieved
future: <Task finished coro=<BaseEventLoop.create_connection() done, defined at /Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py:548> exception=TimeoutError(60, "Connect call failed ('78.174.159.195', 35414)")>
Traceback (most recent call last):
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py", line 241, in _step
result = coro.throw(exc)
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py", line 645, in create_connection
raise exceptions[0]
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py", line 632, in create_connection
yield from self.sock_connect(sock, address)
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 358, in __iter__
yield self # This tells Task to wait for completion.
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py", line 290, in _wakeup
future.result()
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/selector_events.py", line 436, in _sock_connect_cb
raise OSError(err, 'Connect call failed %s' % (address,))
TimeoutError: [Errno 60] Connect call failed ('78.174.159.195', 35414)
Task exception was never retrieved
future: <Task finished coro=<BaseEventLoop.create_connection() done, defined at /Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py:548> exception=TimeoutError(60, "Connect call failed ('85.103.126.106', 22665)")>
Traceback (most recent call last):
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py", line 241, in _step
result = coro.throw(exc)
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py", line 645, in create_connection
raise exceptions[0]
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py", line 632, in create_connection
yield from self.sock_connect(sock, address)
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 358, in __iter__
yield self # This tells Task to wait for completion.
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py", line 290, in _wakeup
future.result()
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/selector_events.py", line 436, in _sock_connect_cb
raise OSError(err, 'Connect call failed %s' % (address,))
TimeoutError: [Errno 60] Connect call failed ('85.103.126.106', 22665)
Task exception was never retrieved
future: <Task finished coro=<BaseEventLoop.create_connection() done, defined at /Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py:548> exception=TimeoutError(60, "Connect call failed ('81.228.224.142', 13570)")>
Traceback (most recent call last):
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py", line 241, in _step
result = coro.throw(exc)
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py", line 645, in create_connection
raise exceptions[0]
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py", line 632, in create_connection
yield from self.sock_connect(sock, address)
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 358, in __iter__
yield self # This tells Task to wait for completion.
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py", line 290, in _wakeup
future.result()
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/selector_events.py", line 436, in _sock_connect_cb
raise OSError(err, 'Connect call failed %s' % (address,))
TimeoutError: [Errno 60] Connect call failed ('81.228.224.142', 13570)
与80.94.76.7:14122连接
接174.110.236.233:45308
接78.177.119.170:27311
接95.15.59.242:21426
断开的。。。
exc:[Errno 54]对等方重置连接
断开的。。。
exc:[Errno 54]对等方重置连接
断开的。。。
行政长官:没有
断开的。。。
行政长官:没有
从未检索到任务异常
未来:
回溯(最近一次呼叫最后一次):
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py”,第241行,步骤
结果=核心投掷(exc)
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base\u events.py”,第645行,在create\u connection中
引发异常[0]
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py”,第632行,在create_connection中
self.sock\u connect(sock,地址)的收益
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py”,第358行,在__
屈服自我——这告诉任务等待完成。
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py”,第290行,在唤醒中
future.result()
结果中的文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py”,第274行
提出自己的意见
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/selector\u events.py”,第436行,在sock\u connect\u cb中
引发操作错误(错误,“连接调用失败%s%”(地址,)
TimeoutError:[Errno 60]连接调用失败('93.34.49.17',13311)
从未检索到任务异常
未来:
回溯(最近一次呼叫最后一次):
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py”,第241行,步骤
结果=核心投掷(exc)
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base\u events.py”,第645行,在create\u connection中
引发异常[0]
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py”,第632行,在create_connection中
self.sock\u connect(sock,地址)的收益
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py”,第358行,在__
屈服自我——这告诉任务等待完成。
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py”,第290行,在唤醒中
future.result()
结果中的文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py”,第274行
提出自己的意见
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/selector\u events.py”,第436行,在sock\u connect\u cb中
引发操作错误(错误,“连接调用失败%s%”(地址,)
TimeoutError:[Errno 60]连接调用失败('197.29.6.31',50735)
从未检索到任务异常
未来:
回溯(最近一次呼叫最后一次):
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py”,第241行,步骤
结果=核心投掷(exc)
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base\u events.py”,第645行,在create\u connection中
引发异常[0]
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py”,第632行,在create_connection中
self.sock\u connect(sock,地址)的收益
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py”,第358行,在__
屈服自我——这告诉任务等待完成。
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py”,第290行,在唤醒中
future.result()
结果中的文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py”,第274行
提出自己的意见
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/selector\u events.py”,第436行,在sock\u connect\u cb中
引发操作错误(错误,“连接调用失败%s%”(地址,)
TimeoutError:[Errno 60]连接调用失败('195.174.165.47',61567)
从未检索到任务异常
未来:
回溯(最近一次呼叫最后一次):
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py”,第241行,步骤
结果=核心投掷(exc)
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base\u events.py”,第645行,在create\u connection中
引发异常[0]
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.py”,第632行,在create_connection中
self.sock\u connect(sock,地址)的收益
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py”,第358行,在__
屈服自我——这告诉任务等待完成。
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py”,第290行,在唤醒中
future.result()
结果中的文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py”,第274行
提出自己的意见
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/selector\u events.py”,第436行,在sock\u connect\u cb中
引发操作错误(错误,“连接调用失败%s%”(地址,)
ConnectionRefusedError:[Errno 61]连接调用失败('69.122.194.81',6881)
从未检索到任务异常
未来:
回溯(最近一次呼叫最后一次):
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py”,第241行,步骤
结果=核心投掷(exc)
文件“/Users/shangsunset/.pyenv/versions/3.5.1/lib/python3.5/asyncio/base_events.p
class Torrent():
# ...
async def connect_to_peer(self, peer):
try:
# await, here exception would be raised
await self.loop.create_connection(
PeerProtocol,
peer['host'],
peer['port']
)
except ConnectionRefusedError:
print('caught')
except TimeoutError:
print('timeout error')
async def connect_to_peers(self): # async function
await asyncio.gather(
*[self.connect_to_peer(peer) for peer in self.peers],
loop=self.loop # fixed here!
)
# Btw, you can add param return_exceptions=True to get exceptions
# in results here instead of ignoring it inside connect_to_peer
# ...
loop.run_until_complete(client.connect_to_peers())