简体   繁体   English

用python获取http响应代码

[英]get http response codes with python

I know how to do this with httplib, but I need to also set the user-agent and I'm sure you need urllib to do that. 我知道如何使用httplib执行此操作,但我还需要设置用户代理,并且我确定您需要urllib来执行此操作。 How can I get the http response codes with urllib? 如何使用urllib获取http响应代码?

You can use .getcode() in urllib2 to get the HTTP code: 您可以在urllib2中使用.getcode()来获取HTTP代码:

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

Full headers with are in info() as a list: 完整标题在info()作为列表:

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

Actually, httplib DOES allow to set User-Agent. 实际上,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