使用httplib2处理身份验证和代理服务器 [英] Handling authentication and proxy servers with httplib2

查看:142
本文介绍了使用httplib2处理身份验证和代理服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试与Nexus服务器的交互,该服务器需要对我要使用的操作进行身份验证,但是我还需要处理内部代理服务器.

I'm attempting to test interactions with a Nexus server that requires authentication for the operations I intend to use, but I also need to handle an internal proxy server.

基于我的(有限的)理解,我可以向启动器添加多个处理程序.但是我仍然收到401响应.我检查了用户名和密码是否有效.我不确定是否需要cookie来执行此操作,以及是否包含cookie. 有什么建议吗?

Based on my (limited) understanding I can add multiple handlers to the opener. However I'm still getting a 401 response. I've checked the username and password are valid. I'm not certain if cookies are required to do this and if so how they'd be included. Any suggestions?

baseUrl = 'server:8070/nexus-webapp-1.3.3/service/local'

params = {"[key]":"[value]"}

data = urllib.urlencode(params)

# create a password manager
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()

# Add the username and password as supplied
password_mgr.add_password(None, baseUrl, username, password)

handler = urllib2.HTTPBasicAuthHandler(password_mgr)
proxy_support = urllib2.ProxyHandler({})

# create "opener" (OpenerDirector instance)
opener = urllib2.build_opener(proxy_support, handler)
urllib2.install_opener(opener)

txheaders =  {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}

req = Request(protocol+url, data, txheaders)
handle = urlopen(req)

这是结果URLError的标题字段:

This is the resulting URLError's headers field:

>HTTPMessage: Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=B4BD05C9582F7B27495CBB675A339724; Path=/nexus-webapp-1.3.3
WWW-Authenticate: NxBASIC realm="Sonatype Nexus Repository Manager API"
Content-Type: text/html;charset=utf-8
Content-Length: 954
Date: Fri, 03 Jul 2009 17:38:42 GMT
Connection: close

更新 Nexus似乎实现了

Update It seems Nexus implement a custom version of Restlet's AuthenticationHelper. Thanks to Alex I knew what to look for.

推荐答案

您能显示所获得的401响应的完整标题吗?也许这不是基本的身份验证请求,可能是代理服务器需要自己的身份验证-很难看到没有所述标头的信息!

Can you show the full headers of the 401 response you're getting? Maybe it's not a basic auth request, maybe it's the proxy wanting its own authentication -- it's hard to guess without seeing said headers!

编辑:感谢您显示标题(我将其重新格式化为代码",否则无法读取).

Edit: thanks for showing the headers (I reformatted them as "code" else they were unreadable).

正如我所怀疑的那样,它不需要基本",而需要其他一些(Nexus专有...?)"NxBASIC"身份验证协议-我从没听说过(我对此一无所知Nexus),我想您都不会使用基本身份验证处理程序(即使NxBASIC某种程度上接受了普通的基本身份验证,该处理程序仅知道它是一个不同的协议,也不会提供这种身份验证).

As I suspected, it doesn't want "Basic", it wants some other (Nexus proprietary...?) "NxBASIC" authentication protocol -- I've never heard about it (I don't know anything about Nexus) and I imagine neither has the basic authentication handler you're using (even if NxBASIC somehow accepted plain Basic authentication, the handler, knowing only that it's a different protocol, would not offer such authentication).

因此,首先,您需要准确研究NxBASIC的含义,为此,我怀疑带有正确标签的SO问题可能会有所帮助.然后,根据您所学的知识,出现了一个有趣的问题,即为其定义处理程序...!-(

So, first you need to research exactly what that NxBASIC thing is, and for that I suspect a SO question with the right tags might help. Then, depending on what you learn, comes the interesting issue of defining a handler for it...!-(

这篇关于使用httplib2处理身份验证和代理服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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