【问题标题】:Authenticating to jenkins with python3 requests module使用 python3 请求模块对詹金斯进行身份验证
【发布时间】:2020-02-14 15:17:53
【问题描述】:

我有以下问题:

我现在通过 curl 从 jenkins 服务器获取状态和其他一些信息,一切正常,我得到了信息。现在想换 如何将其获取到请求库的方式,现在我无法再进行身份验证了。我现在将以下内容用于请求部分:

data ={"Username":"foo_user","Password":"bar_pass"}        
response = session.post("jenkins_url_to_trigger",data=data )

我也尝试使用 http 基本身份验证设置 auth 参数。

如果有人能够通过 requests 库访问 jenkins 服务器,那将是 很高兴看到一个如何做的例子,因为我现在坚持了一段时间。

对于有效的 curl 部分,我使用以下简单语法:

curl -u foo_user:bar_pass -X GET -H "Content-Type: application/json" jenkins_url/something/to/get/api/json --insecure

我也搜索了类似的问题,但没有找到任何对我有帮助的东西。

【问题讨论】:

    标签: python-3.x authentication jenkins python-requests


    【解决方案1】:

    在元组中以auth 形式提供凭据,而不是在字典中以data 形式提供。

    credentials = ('foo_user', 'bar_pass')       
    response = session.post('<jenkins_url_to_trigger>', auth=credentials)
    

    Authentication

    【讨论】:

      【解决方案2】:

      您可以在requests.Session 对象中设置身份验证

      import requests
      
      session = requests.Session()
      session.auth = ('myuser', 'mypass')
      
      resp = session.post('jenkins_url_to_trigger')
      

      Session 对象引用:https://requests.readthedocs.io/en/latest/user/advanced/#session-objects

      【讨论】:

        猜你喜欢
        • 2013-03-28
        • 1970-01-01
        • 2017-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-14
        • 1970-01-01
        相关资源
        最近更新 更多