如何调试使用基本身份验证处理程序的 urllib2 请求
我正在使用 urllib2
和 HTTPBasicAuthHandler
发出请求,如下所示:
import urllib2
theurl = 'http://someurl.com'
username = 'username'
password = 'password'
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, theurl, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
params = "foo=bar"
response = urllib2.urlopen('http://someurl.com/somescript.cgi', params)
print response.info()
运行此代码时,我当前收到 httplib.BadStatusLine
异常。我该如何进行调试?有没有办法无论无法识别的 HTTP 状态代码如何都可以查看原始响应是什么?
I'm making a request using urllib2
and the HTTPBasicAuthHandler
like so:
import urllib2
theurl = 'http://someurl.com'
username = 'username'
password = 'password'
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, theurl, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
params = "foo=bar"
response = urllib2.urlopen('http://someurl.com/somescript.cgi', params)
print response.info()
I'm currently getting a httplib.BadStatusLine
exception when running this code. How could I go about debugging? Is there a way to see what the raw response is regardless of the unrecognized HTTP status code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过在自己的 HTTP 处理程序中设置调试级别?将代码更改为如下所示:
因此要添加的三行是:
这将在 stderr 上显示原始 HTTP 发送/回复周期。
根据评论进行编辑
这有效吗?
Have you tried setting the debug level in your own HTTP handler? Change your code to something like this:
So the three lines to prepend are:
That will show the raw HTTP send / reply cycle on stderr.
Edit from comment
Does this work?