P i*_*P i 5 python exception httpx
我因此陷入困境:
\nwith httpx.Client(**sessions[scraperIndex]) as client:\n try:\n response = client.get(...)\n except TimeoutError as e:\n print(\'does not hit\')\n except Exception as e:\n print(f\'\xe2\x9b\x94\xef\xb8\x8f Unexpected exception: {e}\')\n print_exc() # hits!\nRun Code Online (Sandbox Code Playgroud)\n不过我收到了以下故障转储。
\n拉出关键线:
\nTimeoutError: The read operation timed out\n\nDuring handling of the above exception, another exception occurred:\n httpcore.ReadTimeout: The read operation timed out\n\nThe above exception was the direct cause of the following exception:\n httpx.ReadTimeout: The read operation timed out\nRun Code Online (Sandbox Code Playgroud)\n为什么我没有TimeoutError抓住这个?
正确的做法是什么?有人可以给出推论的逻辑吗?
\n崩溃转储:
\n\xe2\x9b\x94\xef\xb8\x8f Unexpected exception: The read operation timed out\nTraceback (most recent call last):\n File "/usr/local/lib/python3.10/dist-packages/httpcore/_exceptions.py", line 8, in map_exceptions\n yield\n File "/usr/local/lib/python3.10/dist-packages/httpcore/backends/sync.py", line 26, in read\n return self._sock.recv(max_bytes)\n File "/usr/lib/python3.10/ssl.py", line 1258, in recv\n return self.read(buflen)\n File "/usr/lib/python3.10/ssl.py", line 1131, in read\n return self._sslobj.read(len)\nTimeoutError: The read operation timed out\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n File "/usr/local/lib/python3.10/dist-packages/httpx/_transports/default.py", line 60, in map_httpcore_exceptions\n yield\n File "/usr/local/lib/python3.10/dist-packages/httpx/_transports/default.py", line 218, in handle_request\n resp = self._pool.handle_request(req)\n File "/usr/local/lib/python3.10/dist-packages/httpcore/_sync/connection_pool.py", line 253, in handle_request\n raise exc\n File "/usr/local/lib/python3.10/dist-packages/httpcore/_sync/connection_pool.py", line 237, in handle_request\n response = connection.handle_request(request)\n File "/usr/local/lib/python3.10/dist-packages/httpcore/_sync/connection.py", line 90, in handle_request\n return self._connection.handle_request(request)\n File "/usr/local/lib/python3.10/dist-packages/httpcore/_sync/http11.py", line 105, in handle_request\n raise exc\n File "/usr/local/lib/python3.10/dist-packages/httpcore/_sync/http11.py", line 84, in handle_request\n ) = self._receive_response_headers(**kwargs)\n File "/usr/local/lib/python3.10/dist-packages/httpcore/_sync/http11.py", line 148, in _receive_response_headers\n event = self._receive_event(timeout=timeout)\n File "/usr/local/lib/python3.10/dist-packages/httpcore/_sync/http11.py", line 177, in _receive_event\n data = self._network_stream.read(\n File "/usr/local/lib/python3.10/dist-packages/httpcore/backends/sync.py", line 24, in read\n with map_exceptions(exc_map):\n File "/usr/lib/python3.10/contextlib.py", line 153, in __exit__\n self.gen.throw(typ, value, traceback)\n File "/usr/local/lib/python3.10/dist-packages/httpcore/_exceptions.py", line 12, in map_exceptions\n raise to_exc(exc)\nhttpcore.ReadTimeout: The read operation timed out\n\nThe above exception was the direct cause of the following exception:\n\nTraceback (most recent call last):\n File "/root/scraper-pi/Scrape.py", line 148, in main\n cursor, _nScraped = scrape(client, cursor)\n File "/root/scraper-pi/Scrape.py", line 79, in scrape\n response = client.get(\n File "/usr/local/lib/python3.10/dist-packages/httpx/_client.py", line 1039, in get\n return self.request(\n File "/usr/local/lib/python3.10/dist-packages/httpx/_client.py", line 815, in request\n return self.send(request, auth=auth, follow_redirects=follow_redirects)\n File "/usr/local/lib/python3.10/dist-packages/httpx/_client.py", line 902, in send\n response = self._send_handling_auth(\n File "/usr/local/lib/python3.10/dist-packages/httpx/_client.py", line 930, in _send_handling_auth\n response = self._send_handling_redirects(\n File "/usr/local/lib/python3.10/dist-packages/httpx/_client.py", line 967, in _send_handling_redirects\n response = self._send_single_request(request)\n File "/usr/local/lib/python3.10/dist-packages/httpx/_client.py", line 1003, in _send_single_request\n response = transport.handle_request(request)\n File "/usr/local/lib/python3.10/dist-packages/httpx/_transports/default.py", line 217, in handle_request\n with map_httpcore_exceptions():\n File "/usr/lib/python3.10/contextlib.py", line 153, in __exit__\n self.gen.throw(typ, value, traceback)\n File "/usr/local/lib/python3.10/dist-packages/httpx/_transports/default.py", line 77, in map_httpcore_exceptions\n raise mapped_exc(message) from exc\nhttpx.ReadTimeout: The read operation timed out\nRun Code Online (Sandbox Code Playgroud)\n
所有超时错误的基类httpx不是内置的TimeoutError(大概是因为这也会导致超时OSError,这听起来不正确),而是httpx.TimeoutException.
import httpx
with httpx.Client() as client:
try:
response = client.get("http://httpbin.org/get", timeout=0.001)
except httpx.TimeoutException as e:
print('gottem')
Run Code Online (Sandbox Code Playgroud)
打印gottem得很好。
| 归档时间: |
|
| 查看次数: |
3746 次 |
| 最近记录: |