Python urllib2 基本身份验证问题 [英] Python urllib2 Basic Auth Problem

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

问题描述

更新:根据 Lee 的评论,我决定将我的代码压缩为一个非常简单的脚本并从命令行运行它:

Update: based on Lee's comment I decided to condense my code to a really simple script and run it from the command line:

import urllib2
import sys

username = sys.argv[1]
password = sys.argv[2]
url = sys.argv[3]
print("calling %s with %s:%s
" % (url, username, password))

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, username, password)
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman)))

req = urllib2.Request(url)
f = urllib2.urlopen(req)
data = f.read()
print(data)

不幸的是,它仍然不会生成 Authorization 标头(每个 Wireshark):(

Unfortunately it still won't generate the Authorization header (per Wireshark) :(

我在通过 urllib2 发送基本 AUTH 时遇到问题.我查看了这篇文章,并按照示例进行操作.我的代码:

I'm having a problem sending basic AUTH over urllib2. I took a look at this article, and followed the example. My code:

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, "api.foursquare.com", username, password)
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman)))

req = urllib2.Request("http://api.foursquare.com/v1/user")    
f = urllib2.urlopen(req)
data = f.read()

我通过 Wireshark 在 Wire 上看到以下内容:

I'm seeing the following on the Wire via wireshark:

GET /v1/user HTTP/1.1
Host: api.foursquare.com
Connection: close
Accept-Encoding: gzip
User-Agent: Python-urllib/2.5 

您可以看到授权未发送,而当我通过 curl 发送请求时:curl -u user:password http://api.foursquare.com/v1/user

You can see the Authorization is not sent, vs. when I send a request via curl: curl -u user:password http://api.foursquare.com/v1/user

GET /v1/user HTTP/1.1
Authorization: Basic =SNIP=
User-Agent: curl/7.19.4 (universal-apple-darwin10.0) libcurl/7.19.4 OpenSSL/0.9.8k zlib/1.2.3
Host: api.foursquare.com
Accept: */*

出于某种原因,我的代码似乎没有发送身份验证 - 有人看到我遗漏了什么吗?

For some reason my code seems to not send the authentication - anyone see what I'm missing?

谢谢

-西蒙

推荐答案

问题可能是 Python 库,根据 HTTP 标准,首先发送一个未经身份验证的请求,然后只有当它以 401 重试得到答复时,发送的正确凭据.如果 Foursquare 服务器不进行完全标准的身份验证",那么库将无法工作.

The problem could be that the Python libraries, per HTTP-Standard, first send an unauthenticated request, and then only if it's answered with a 401 retry, are the correct credentials sent. If the Foursquare servers don't do "totally standard authentication" then the libraries won't work.

尝试使用标头进行身份验证:

Try using headers to do authentication:

import urllib2, base64

request = urllib2.Request("http://api.foursquare.com/v1/user")
base64string = base64.b64encode('%s:%s' % (username, password))
request.add_header("Authorization", "Basic %s" % base64string)   
result = urllib2.urlopen(request)

和你有同样的问题,并从这个线程找到了解决方案:http://forums.shopify.com/categories/9/posts/27662

Had the same problem as you and found the solution from this thread: http://forums.shopify.com/categories/9/posts/27662

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

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