python - Jenkins python api 身份验证 "403 forbidden"

标签 python jenkins urllib2

我正在尝试使用 jenkinsapi python library与 Jenkins 一起工作。我的 Jenkins 设置为使用 LDAP 进行身份验证(使用 Active Directory Plugin )。但是,当我运行我的脚本进行身份验证时,我失败了(实际上它运行了一次,但从那以后我每次都收到 403 Forbidden 错误)

它从一开始就失败了:

from jenkinsapi.jenkins import Jenkins
j = Jenkins("https://qaa.server.com/jenkins", "username", "password")

Traceback (most recent call last):
  File "C:\eclipse-SDK-4.2-win32-x86_64\eclipse\plugins\org.python.pydev_2.6.0.2012062818\pysrc\pydevd.py", line 1392, in <module>
    debugger.run(setup['file'], None, None)
  File "C:\eclipse-SDK-4.2-win32-x86_64\eclipse\plugins\org.python.pydev_2.6.0.2012062818\pysrc\pydevd.py", line 1085, in run
    pydev_imports.execfile(file, globals, locals) #execute the script
  File "C:\work\selenium-project\ses-web\utils\mark_slave_offline.py", line 10, in <module>
    j = Jenkins("https://qaa.server.com/jenkins", "username", "password")
  File "C:\Python27\lib\site-packages\jenkinsapi-0.1.11-py2.7.egg\jenkinsapi\jenkins.py", line 43, in __init__
    JenkinsBase.__init__(self, baseurl, formauth=formauth)
  File "C:\Python27\lib\site-packages\jenkinsapi-0.1.11-py2.7.egg\jenkinsapi\jenkinsbase.py", line 34, in __init__
    self.poll()
  File "C:\Python27\lib\site-packages\jenkinsapi-0.1.11-py2.7.egg\jenkinsapi\jenkinsbase.py", line 41, in poll
    self._data = self._poll()
  File "C:\Python27\lib\site-packages\jenkinsapi-0.1.11-py2.7.egg\jenkinsapi\jenkinsbase.py", line 45, in _poll
    return retry_function(self.RETRY_ATTEMPTS , self.get_data, url)
  File "C:\Python27\lib\site-packages\jenkinsapi-0.1.11-py2.7.egg\jenkinsapi\utils\retry.py", line 39, in retry_function
    raise e
urllib2.HTTPError: HTTP Error 403: Forbidden

有趣的是,以下代码每次都能正常工作:

def urlopen(url,username=None, password=None, data=None):
    '''Open a URL using the urllib2 opener.'''
    request = urllib2.Request(url, data)
    base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
    request.add_header("Authorization", "Basic %s" % base64string)
    response = urllib2.urlopen(request)
    return response

result = urlopen('https://qaa.server.com/jenkins', "username", "password")

问题:为什么 Jenkins 代码会失败?会不会是apache服务器配置错误?

谢谢!

更新:这是我收到的回复

<html>
    <head>
        <meta http-equiv='refresh' content='1;url=/jenkins/loginfrom=%2Fjenkins%2Fapi%2Fpython%2F'/>
        <script>window.location.replace('/jenkins/login?from=%2Fjenkins%2Fapi%2Fpython%2F');
        </script>
    </head>
<body style='background-color:white; color:white;'>Authentication required</body>
</html>      

Apache 访问日志:- [07/Sep/2012:15:24:21 -0400] "GET/jenkins/api/python/HTTP/1.1"403 593

更新: Debug模式下的错误日志:

.....
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1897): OpenSSL: read 160/160 bytes from BIO#e08150 [mem: e96ea5] (BIO dump follows)
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1830): +-------------------------------------------------------------------------+
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1869): | 0000: 64 c2 ac 70 d0 d6 22 02-ef 56 98 32 6e 16 20 53  d..p.."..V.2n. S |
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1869): | 0010: 84 fb 94 c7 51 a4 a8 9c-b3 8f 48 c8 84 1e 46 6b  ....Q.....H...Fk |
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1869): | 0020: 15 c9 f4 cb ef cf 38 ed-a1 8b 41 08 a6 3b 95 f0  ......8...A..;.. |
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1869): | 0030: bd b3 05 ef 68 68 e0 fb-52 92 ad fc 7f aa 72 50  ....hh..R.....rP |
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1869): | 0040: 9e d9 7d 98 18 82 eb 07-bb 45 e3 e5 b2 93 6d 34  ..}......E....m4 |
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1869): | 0050: fe 18 b5 d4 5c c9 78 c9-78 0c 6b cb 50 80 96 6d  ....\\.x.x.k.P..m |
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1869): | 0060: 49 8a 39 b2 73 b2 ae 76-a4 d3 50 7a 38 a7 54 72  I.9.s..v..Pz8.Tr |
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1869): | 0070: 41 e5 13 b2 1f 71 a1 b0-db 89 81 4f 14 3a 93 18  A....q.....O.:.. |
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1869): | 0080: 5a e0 0e 9e c9 d9 1e f3-01 53 eb 08 dc 23 89 fd  Z........S...#.. |
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1869): | 0090: a0 13 81 ac 26 55 54 fd-22 64 7e 37 f3 cd 2d af  ....&UT."d~7..-. |
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1875): +-------------------------------------------------------------------------+
[Fri Sep 07 16:52:09 2012] [info] Initial (No.1) HTTPS request received for child 63 (server qaa.server.com:443)
[Fri Sep 07 16:52:09 2012] [debug] mod_proxy_http.c(56): proxy: HTTP: canonicalising URL //localhost:8180/jenkins/api/python/
[Fri Sep 07 16:52:09 2012] [debug] proxy_util.c(1506): [client 207.***.***.1*2] proxy: http: found worker http://localhost:8180/jenkins for http://localhost:8180/jenkins/api/python/
[Fri Sep 07 16:52:09 2012] [debug] mod_proxy.c(1015): Running scheme http handler (attempt 0)
[Fri Sep 07 16:52:09 2012] [debug] mod_proxy_http.c(1973): proxy: HTTP: serving URL http://localhost:8180/jenkins/api/python/
[Fri Sep 07 16:52:09 2012] [debug] proxy_util.c(2011): proxy: HTTP: has acquired connection for (localhost)
[Fri Sep 07 16:52:09 2012] [debug] proxy_util.c(2067): proxy: connecting http://localhost:8180/jenkins/api/python/ to localhost:8180
[Fri Sep 07 16:52:09 2012] [debug] proxy_util.c(2193): proxy: connected /jenkins/api/python/ to localhost:8180
[Fri Sep 07 16:52:09 2012] [debug] proxy_util.c(2444): proxy: HTTP: fam 2 socket created to connect to localhost
[Fri Sep 07 16:52:09 2012] [debug] proxy_util.c(2576): proxy: HTTP: connection complete to 127.0.0.1:8180 (localhost)
[Fri Sep 07 16:52:09 2012] [debug] mod_proxy_http.c(1743): proxy: start body send
[Fri Sep 07 16:52:09 2012] [debug] mod_proxy_http.c(1847): proxy: end body send
[Fri Sep 07 16:52:09 2012] [debug] proxy_util.c(2029): proxy: HTTP: has released connection for (localhost)
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_kernel.c(1884): OpenSSL: Write: SSL negotiation finished successfully
[Fri Sep 07 16:52:09 2012] [info] [client 207.***.***.1*2] Connection closed to child 63 with standard shutdown (server qaa.server.com:443)

最佳答案

此问题的解决方案是使用不同的 python-jenkins module

使用这个模块一切正常:

import jenkins
j = jenkins.Jenkins(base_url, username, password)

关于python - Jenkins python api 身份验证 "403 forbidden",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12322668/

相关文章:

python - pylint 解析器 - Jenkins 违规插件的独立替代方案

python - Appengine搜索中的多个字段名称

python - 用 Python 中之前出现的数字填充空列

Jenkins:忽略管道构建步骤中的失败

java - 如何在 Jenkins 管道中导入 Java 库

python - 使用 Python/urllib/beautifulsoup 从 URL 批量下载文本和图像?

python - 如何防止 Python 的 urllib(2) 跟随重定向

Python urllib2 恢复下载在网络重新连接时不起作用

python - Pandas :枚举索引中的重复项

list - Python:将项目添加到列表直到条件为真