Python请求异常超时

Python请求异常超时,python,exception,error-handling,timeout,Python,Exception,Error Handling,Timeout,我编写了一个python脚本,如下所示: #!/usr/bin/python import sys import requests if len(sys.argv) != 2: print "Usage: python %s <IP-LIST>" % (sys.argv[0]) sys.exit(); InputFile = sys.argv[1] try: File = open(InputFile) for ip in File

我编写了一个python脚本,如下所示:

#!/usr/bin/python
import sys
import requests

if len(sys.argv) != 2:
        print "Usage: python %s <IP-LIST>" % (sys.argv[0])
        sys.exit();

InputFile = sys.argv[1]
try:
    File = open(InputFile)
    for ip in File:
        IP = ip.rstrip()
        out = requests.get("http://" + IP, timeout=5)
        out.status_code

except (KeyboardInterrupt, SystemExit):
    print "\nKeyboardInterruption with Ctrl+c signal"
except IOError as e:
    print "The file \"%s\" does not exist!" % (sys.argv[1])

为什么?

请求
使用子类
IOError
的异常,您正在捕获并假设未找到该异常。如果您使用的是Python3,则可以捕获更具体的
FileNotFoundError
。在这里,你应该在你的
Exception IOError
块上方放置一个
Exception.RequestException
块(如果你愿意,可以将其分解为特定的
请求
错误。

我收到了,谢谢你……但是你能解释一下“如果你愿意,可以将其分解为特定的请求错误”吗如果你有例子的话,我会很感激……:)我只是说在
请求中有大约六个异常。基本异常类是
RequestException
,以便捕获所有异常。
The file "list.txt" does not exist!