繁体   English   中英

用python获取http响应代码

[英]get http response codes with python

我知道如何使用httplib执行此操作,但我还需要设置用户代理,并且我确定您需要urllib来执行此操作。 如何使用urllib获取http响应代码?

您可以在urllib2中使用.getcode()来获取HTTP代码:

urllib2.urlopen("http://google.com").getcode()

完整标题在info()作为列表:

urllib2.urlopen("http://google.com").info().headers

实际上,httplib允许设置User-Agent。

headers = { 'User-Agent' : 'someapp', 'Content-Type' : 'text/html' }  
conn = httplib.HTTPConnection(host, port)  
conn.request('POST', '/foobar', 'mydata', headers)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM