Python会话不适用于多个请求

时间:2019-09-17 19:51:49

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

我是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]>

1 个答案:

答案 0 :(得分:0)

确定了有效载荷格式的问题。

由于某种原因,以下串联代码无法正常工作:

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"}]}'

我希望这对某人有帮助。