Python的urllib2的基本认证问题

编程入门 行业动态 更新时间:2024-10-28 04:23:59
本文介绍了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", username, password) urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman))) req = urllib2.Request("api.foursquare/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 Connection: close Accept-Encoding: gzip User-Agent: Python-urllib/2.5

您可以看到授权不发送,与当我通过卷曲发送一个请求:卷曲-u用户名:密码的api.foursquare/v1/user

You can see the Authorization is not sent, vs. when I send a request via curl: curl -u user:password api.foursquare/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 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("api.foursquare/v1/user") # You need the replace to handle encodestring adding a trailing newline # (docs.python/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)

有同样的问题,你发现从这个线程解决方案:forums.shopify/categories/9/posts/27662

更多推荐

Python的urllib2的基本认证问题

本文发布于:2023-10-10 01:06:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1477274.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!