Python的urllib2的基本认证问题 [英] Python urllib2 Basic Auth Problem

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

问题描述

更新:根据李的评论,我决定我的凝结code到一个非常简单的脚本和命令行运行它:

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\n" % (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)

不幸的是它仍然不会产生(每Wireshark的)的授权头:(

我有过的urllib2发送基本身份验证的一个问题。我看了看这篇文章,跟着例子。我的code:

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的看到电线上的以下内容:

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 

您可以看到授权不发送,与当我通过卷曲发送一个请求:卷曲-u用户名:密码的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: */*

出于某种原因,我的code似乎没有发送身份验证 - ?任何人看到我错过了什么。

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

感谢

-simon

推荐答案

这个问题可能是,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.

试着用头做认证:

import urllib2, base64

request = urllib2.Request("http://api.foursquare.com/v1/user")
# You need the replace to handle encodestring adding a trailing newline 
# (https://docs.python.org/2/library/base64.html#base64.encodestring)
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)   
result = urllib2.urlopen(request)

有同样的问题,你发现从这个线程解决方案:<一href=\"http://forums.shopify.com/categories/9/posts/27662\">http://forums.shopify.com/categories/9/posts/27662

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

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