Python会话不适用于多个请求
我是python新手,请原谅我缺乏良知 要求:需要通过RESTAPI调用处理字符串列表 问题: 使用请求,我能够成功地进行api调用并完成工作。使用请求没有问题,只是想研究在每个请求中传递AUTH时,使用会话是否会加快处理速度 在使用会话时遵循了多条线索,但继续得到HTTP 400-错误请求 参考: 以下是我正在努力使其工作的代码,任何帮助都将非常有用:Python会话不适用于多个请求,python,python-3.x,session,python-requests,Python,Python 3.x,Session,Python Requests,我是python新手,请原谅我缺乏良知 要求:需要通过RESTAPI调用处理字符串列表 问题: 使用请求,我能够成功地进行api调用并完成工作。使用请求没有问题,只是想研究在每个请求中传递AUTH时,使用会话是否会加快处理速度 在使用会话时遵循了多条线索,但继续得到HTTP 400-错误请求 参考: 以下是我正在努力使其工作的代码,任何帮助都将非常有用: import requests from requests.auth import HTTPBasicAuth with open('lis
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
失败==>
确定了有效负载格式的问题
由于某些原因,下面的连接代码不起作用:
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"}]}'
我希望这对某人有所帮助。发现了有效负载格式的问题 由于某些原因,下面的连接代码不起作用:
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"}]}'
我希望这对某人有所帮助。但是相同的负载可以很好地处理请求。此代码可以正常工作:
response=requests.put(“https://ucdeploy.domain.com/rest/deploy/component,data=payload,auth=HTTPBasicAuth('username','password'))
但相同的有效负载可以很好地处理请求此代码可以正常工作:response=requests.put(“https://ucdeploy.domain.com/rest/deploy/component,data=payload,auth=HTTPBasicAuth('username','password'))