urllib2 基本认证奇数 [英] urllib2 basic authentication oddites

查看:25
本文介绍了urllib2 基本认证奇数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正用这个头撞墙.我一直在尝试每个示例,阅读我可以在网上找到的有关 urllib2 的基本 http 授权的最后一点,但我无法弄清楚是什么导致了我的特定错误.

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.

更令人沮丧的是,该代码适用于一个页面,但不适用于另一个页面.登录 www.mysite.com/adm 非常顺利.它验证没有问题.但是,如果我将地址更改为http://mysite.com/adm/items.php?n=201105&c=200",我会收到此错误:

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.

adm 是一个框架集页面,我不确定这是否相关.

The adm is a frame set page, I'm not sure if that's relevant at all.

这是当前代码:

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 中的每个示例,但仍然收到相同的错误.

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?

推荐答案

大约一年前,我经历了相同的过程并记录了我如何解决问题 - 直接和简单的身份验证方法和标准方法.选择您认为合适的.

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.

Python 中的 HTTP 身份验证

缺少 urllib2 文档中有一个解释说明.

这篇关于urllib2 基本认证奇数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆