我有多个 URL,我想检查 URL 状态等状态(返回代码如 200,404 等)。下面是获取代码的代码。我想检查同一代码的响应时间和长度。谁能帮我修改下面的代码来查找多个网址的响应时间、长度和 getcode。
import socket
from urllib2 import urlopen, URLError, HTTPError
import urllib
socket.setdefaulttimeout( 23 ) # timeout in seconds
#print "---------URL----------", " ---Status Code---"
url='https://www.google.com'
try :
response = urlopen( url )
except HTTPError, e:
print 'The server couldn\'t fulfill the request. Reason:', str(e.code)
#Want to get code for that but its not showing
except URLError, e:
print 'We failed to reach a server. Reason:', str(e.reasonse)
#Want to get code for that but its not showing
else :
code=urllib.urlopen(url).getcode()
**#here getcode is working
print url,"-------->", code
#print 'got response!'
最佳答案
我认为您的用例更适合请求库。使用 pip install requests
安装它并查看 documentation 。您应该立即启动并运行!
关于python - 使用 Python 检查响应时间、长度和 URL 状态(获取代码 200,404 等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36170105/