我想用python-requests
來檢查過期的域名。準確地捕捉到與Python請求的DNS錯誤
import requests
try:
status = requests.head('http://wowsucherror')
except requests.ConnectionError as exc:
print(exc)
此代碼看起來過於通用。它產生的輸出如下:
HTTPConnectionPool(host='wowsucherror', port=80): Max retries exceeded with url:/(Caused by NewConnectionError(': Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))
我希望做的是隻捕獲這個DNS錯誤(如ERR_NAME_NOT_RESOLVED
在Chrome)。作爲最後的手段,我可以做字符串匹配,但也許有更好,更結構化和前向兼容的方式來處理這個錯誤?
理想情況下,它應該是一些DNSError
擴展到requests
。
UPDATE:Linux上的錯誤是不同的。
HTTPConnectionPool(host='wowsucherror', port=80): Max retries exceeded with url:/(Caused by NewConnectionError(': Failed to establish a new connection: [Errno -2] Name or service not known',))
報告的bug到requests
- >urllib3
https://github.com/shazow/urllib3/issues/1003
UPDATE2:OS X還報告不同的錯誤。
requests.exceptions.ConnectionError: HTTPConnectionPool(host='wowsucherror', port=80): Max retries exceeded with url:/(Caused by NewConnectionError(': Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',))
我想你會被卡住解析字符串中的錯誤號,插座錯誤抓到這裏來https://github.com/kennethreitz/requests/blob/master/requests/packages/urllib3/connection.py #L149但沒有errno屬性設置在任何地方,所以你得到的只是錯誤信息。如果你只是檢查e.errno,你實際上可以訪問'e'。 –
@PadraicCunningham它也看起來像它不是跨平臺的錯誤消息,我需要知道它在Linux和OS X上的外觀。 –
確實,它在我的Ubuntu盒子上拋出了[errno -2],我試過'除了(NewConnectionError,socket.error)爲exc:'但套接字錯誤被吞噬了。可能值得開始一個問題,因爲它似乎是一個合理的事情要做,這只是一個傳遞e.errno的問題。 –