【问题标题】:Python Session not working for multiple requestsPython 会话不适用于多个请求
【发布时间】:2019-09-17 19:51:49
【问题描述】:

我是python新手,所以请原谅我缺乏良心..

需求:需要通过rest api调用处理字符串列表

问题: 使用请求,我能够成功地进行 api 调用并完成工作。使用请求没有问题,除了想研究使用会话是否会加快我的处理速度,因为我在每个请求中都传递了 AUTH。

在使用会话时跟踪了多个潜在客户,但继续收到 HTTP 400 - 错误请求。

参考:Python Requests and persistent sessions

这是我试图使其工作的代码,任何帮助都会非常有帮助:

import requests
from requests.auth import HTTPBasicAuth

with open('list.txt') as f:
    lines = f.read().splitlines()

s = requests.Session()
r = s.get("https://ucdeploy.domain.com/rest/state", auth=HTTPBasicAuth('username', 'password'))

if r:
    print("Logged in Successfully")
else:
    print("Login Failed ==> " + str(r))
    exit()

for cmp in lines:
    print("Processing - " + cmp.strip())
    payload = '{"name":"' + cmp.strip() + '","description":"","templateId":"141e248c-ca02-4f51-a873-c07acd5366cd",' \
                                          '"templateVersion":"","componentType":"STANDARD","sourceConfigPlugin":"",' \
                                          '"importAutomatically":"false","useVfs":"true","defaultVersionType":"FULL",' \
                                          '"importAgentType":"inherit","inheritSystemCleanup":"true",' \
                                          '"runVersionCreationProcess":"false","properties":{},"teamMappings":[{' \
                                          '"teamId":"bf3c4566-160b-4d79-b47e-5f2bbc1ffbb0"}]}'

    response = s.put("https://ucdeploy.domain.com/rest/deploy/component", data=payload)
    if response:
        print("Successfully Processed")
    else:
        print("Failed ==> " + str(response))
        exit()

输出:

Logged in Successfully
Processing - Component1
Failed ==> <Response [400]>

【问题讨论】:

  • 但是相同的有效负载适用于请求此代码没有问题:response = requests.put("https://ucdeploy.domain.com/rest/deploy/component", data=payload, auth=HTTPBasicAuth('username', 'password'))

标签: python python-3.x session python-requests


【解决方案1】:

确定了负载格式的问题。

由于某种原因,下面的连接代码不起作用:

payload = '{"name":"' + cmp.strip() + '","description":"","templateId":"141e248c-ca02-4f51-a873-c07acd5366cd",' \
                                          '"templateVersion":"","componentType":"STANDARD","sourceConfigPlugin":"",' \
                                          '"importAutomatically":"false","useVfs":"true","defaultVersionType":"FULL",' \
                                          '"importAgentType":"inherit","inheritSystemCleanup":"true",' \
                                          '"runVersionCreationProcess":"false","properties":{},"teamMappings":[{' \
                                          '"teamId":"bf3c4566-160b-4d79-b47e-5f2bbc1ffbb0"}]}'

使用以下内容更新了有效负载连接并且它可以工作:

componentname = '{"name":"' + cmp.strip()
payload = componentname + '","description":"","templateId":"141e248c-ca02-4f51-a873-c07acd5366cd",' \
                              '"templateVersion":"","componentType":"STANDARD","sourceConfigPlugin":"",' \
                              '"importAutomatically":"false","useVfs":"true","defaultVersionType":"FULL",' \
                              '"importAgentType":"inherit","inheritSystemCleanup":"true",' \
                              '"runVersionCreationProcess":"false","properties":{},"teamMappings":[{' \
                              '"teamId":"bf3c4566-160b-4d79-b47e-5f2bbc1ffbb0"}]}'

我希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2014-10-09
    相关资源
    最近更新 更多