我有多个 URL,我想检查状态,如 URL 状态(返回代码,如 200,404 等)。下面是将获取代码的代码。我想检查响应时间,同一代码的长度。任何人都可以帮助我修改以下代码以查找多个 url 的响应时间、长度和获取代码。
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!'
请您参考如下方法:
我认为您的用例更适合 Requests 库。使用 pip install requests 安装它并检查 documentation .您应该立即启动并运行!
