相关疑难解决方法(0)

使用urllib2从基于身份验证的Jenkins服务器获取URL

我正在尝试从Jekins服务器获取URL.直到最近,我才能使用此页面上描述的模式(HOWTO使用urllib2获取Internet资源)来创建一个密码管理器,该管理器使用用户名和密码正确响应BasicAuth挑战.一切都很好,直到Jenkins团队改变他们的安全模型,并且该代码不再有效.

# DOES NOT WORK!
import urllib2
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
top_level_url = "http://localhost:8080"

password_mgr.add_password(None, top_level_url, 'sal', 'foobar')
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)

a_url = 'http://localhost:8080/job/foo/4/api/python'
print opener.open(a_url).read()
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

Traceback (most recent call last):
  File "/home/sal/workspace/jenkinsapi/src/examples/password.py", line 11, in <module>
    print opener.open(a_url).read()
  File "/usr/lib/python2.7/urllib2.py", line 410, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 448, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 382, …
Run Code Online (Sandbox Code Playgroud)

python authentication http urllib2 jenkins

1
推荐指数
1
解决办法
7899
查看次数

标签 统计

authentication ×1

http ×1

jenkins ×1

python ×1

urllib2 ×1