urllib2 基本身份验证怪人
我正用这个把头撞在墙上。我一直在尝试每个示例,阅读我可以在网上找到的有关 urllib2 基本 http 授权的所有最后一点,但我无法弄清楚是什么导致了我的特定错误。
更令人沮丧的是,该代码适用于一个页面,但不适用于另一页面。 登录 www.mysite.com/adm 绝对顺利。验证没有问题。然而,如果我将地址更改为“http://mysite.com/adm/items.php?n=201105&c=200”,我会收到此错误:
<h4 align="center" class="teal">Add/Edit Items</h4>
<p><strong>Client:</strong> </p><p><strong>Event:</strong> </p><p class="error">Not enough information to complete this task</p>
<p class="error">This is a fatal error so I am exiting now.</p>
搜索谷歌导致此错误的信息为零。
adm 是一个框架集页面,我不确定这是否相关。
这是当前的代码:
import urllib2, urllib
import sys
import re
import base64
from urlparse import urlparse
theurl = 'http://xxxxxmedia.com/adm/items.php?n=201105&c=200'
username = 'XXXX'
password = 'XXXX'
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, theurl,username,password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
pagehandle = urllib2.urlopen(theurl)
url = 'http://xxxxxxxmedia.com/adm/items.php?n=201105&c=200'
values = {'AvAudioCD': 1,
'AvAudioCDDiscount': 00, 'AvAudioCDPrice': 50,
'ProductName': 'python test', 'frmSubmit': 'Submit' }
#opener2 = urllib2.build_opener(urllib2.HTTPCookieProcessor())
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
这只是我尝试过的众多版本之一。我已经遵循了 Urllib2 Missing Manual 中的每个示例,但仍然收到相同的错误。
谁能指出我做错了什么?
I'm slamming my head against the wall with this one. I've been trying every example, reading every last bit I can find online about basic http authorization with urllib2, but I can not figure out what is causing my specific error.
Adding to the frustration is that the code works for one page, and yet not for another.
logging into www.mysite.com/adm goes absolutely smooth. It authenticates no problem. Yet if I change the address to 'http://mysite.com/adm/items.php?n=201105&c=200' I receive this error:
<h4 align="center" class="teal">Add/Edit Items</h4>
<p><strong>Client:</strong> </p><p><strong>Event:</strong> </p><p class="error">Not enough information to complete this task</p>
<p class="error">This is a fatal error so I am exiting now.</p>
Searching google has lead to zero information on this error.
The adm is a frame set page, I'm not sure if that's relevant at all.
Here is the current code:
import urllib2, urllib
import sys
import re
import base64
from urlparse import urlparse
theurl = 'http://xxxxxmedia.com/adm/items.php?n=201105&c=200'
username = 'XXXX'
password = 'XXXX'
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, theurl,username,password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
pagehandle = urllib2.urlopen(theurl)
url = 'http://xxxxxxxmedia.com/adm/items.php?n=201105&c=200'
values = {'AvAudioCD': 1,
'AvAudioCDDiscount': 00, 'AvAudioCDPrice': 50,
'ProductName': 'python test', 'frmSubmit': 'Submit' }
#opener2 = urllib2.build_opener(urllib2.HTTPCookieProcessor())
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
This is just one of the many versions I've tried. I've followed every example from Urllib2 Missing Manual but still receive the same error.
Can anyone point to what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
今天遇到类似的问题。我在正在开发的网站上使用基本身份验证,但无法对任何用户进行身份验证。
您可以使用以下一些内容来调试问题:
总而言之,您的应用程序可能是正确的,但您可能不会返回客户端发送凭据的标准标头和状态代码。使用调试工具来查找哪个是哪个。另外,httplib2 还有调试模式,只需设置 httplib2.debuglevel=1 即可将调试信息打印在标准输出上。这比使用 tcpdump 更有帮助,因为它处于更高的级别。
希望这对某人有帮助。
Run into a similar problem today. I was using basic authentication on the website I am developing and I couldn't authenticate any users.
Here are a few things you can use to debug your problem:
So to sum up, your application may be correct but you might not be returning the standard headers and status code for the client to send credentials. Use your debug tools to find which is which. Also, there's debug mode for httplib2, just set
httplib2.debuglevel=1
so that debug information is printed on the standard output. This is much more helpful then using tcpdump because it is at a higher level.Hope this helps someone.
大约一年前,我经历了相同的过程并记录了我如何解决问题 - 直接而简单的身份验证方法和标准方法。选择您认为合适的。
Python 中的 HTTP 身份
验证 在 缺少 urllib2 文档。
About an year ago, I went thro' the same process and documented how I solved the problem - The direct and simple way to authentication and the standard one. Choose what you deem fit.
HTTP Authentication in Python
There is an explained description, in the missing urllib2 document.
从您发布的 HTML 来看,它仍然认为您身份验证成功,但随后在处理您的 POST 请求时遇到错误。我尝试了你的 URL,但身份验证失败,我得到一个标准的 401 页面。
无论如何,我建议您再次尝试运行代码并在 Firefox 中手动执行相同的操作,只是这次使用 Wireshark 来捕获交换。您可以获取两种情况下 HTTP 请求和响应的全文并比较差异。在大多数情况下,这将引导您找到错误的根源。
From the HTML you posted, it still think that you authenticate successfully but encounter an error afterwards, in the processing of your POST request. I tried your URL and failing authentication, I get a standard 401 page.
In any case, I suggest you try again running your code and performing the same operation manually in Firefox, only this time with Wireshark to capture the exchange. You can grab the full text of the HTTP request and response in both cases and compare the differences. In most cases that will lead you to the source of the error you get.
我还发现路人的东西不起作用(有时?)。按照此答案添加base64用户/通行标头 https://stackoverflow.com/a/18592800/623159 确实有效为我。我正在访问 jenkins URL,如下所示: http:///job//lastCompletedBuild/testR eport/api/python
这对我有用:
这对我不起作用,每次都会出现错误 403:
I also found the passman stuff doesn't work (sometimes?). Adding the base64 user/pass header as per this answer https://stackoverflow.com/a/18592800/623159 did work for me. I am accessing jenkins URL like this: http:///job//lastCompletedBuild/testReport/api/python
This works for me:
This doesn't work for me, error 403 each time: