Python等价于Curl,提供400响应
正在尝试使用请求模块将shell curl重新写入python脚本 我的shell脚本:Python等价于Curl,提供400响应,python,api,curl,python-requests,http-status-code-400,Python,Api,Curl,Python Requests,Http Status Code 400,正在尝试使用请求模块将shell curl重新写入python脚本 我的shell脚本: #!/bin/bash ip=$1 url=https://xxx.xx.xx.com secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx expand=computerStatus curl --silent -X POST "$url/api/computers/search?expand=$expand" -H "Content-Typ
#!/bin/bash
ip=$1
url=https://xxx.xx.xx.com
secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
expand=computerStatus
curl --silent -X POST "$url/api/computers/search?expand=$expand" -H "Content-Type: application/json" -H "api-secret-key: $secret" -H "api-version: v1" -d '{"maxItems": 10,"searchCriteria": [{"fieldName": "hostName","stringTest": "equal", "stringValue": "'"$ip"'"}]}' -k > $file
上面的代码在bash中运行良好
我正在尝试转换为类似的python等价物
我试过的
import json
import requests
import sys
ip = sys.argv[1]
sys_info = []
url=https://xxx.xx.xx.com
secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
expand=computerStatus
headers = {
'Content-Type': 'application/json',
'api-secret-key': secret,
'api-version': 'v1',
}
params = (
('expand', 'expand'),
)
data = '{"maxItems": 10,"searchCriteria": [{"fieldName": "hostName","stringTest": "equal", "stringValue": ip}]}'
response = requests.post('https://url/api/computers/search?expand=expand', headers=headers, params=params, data=data)
print(response)
<Response [400]>
导入json
导入请求
导入系统
ip=sys.argv[1]
系统信息=[]
网址=https://xxx.xx.xx.com
机密=xxxxxxxxxxxxxxxxxxxxxxxxxxxx
展开=计算机状态
标题={
“内容类型”:“应用程序/json”,
“api密钥”:机密,
“api版本”:“v1”,
}
参数=(
(“展开”、“展开”),
)
数据=“{“maxItems”:10,“searchCriteria”:[{“fieldName”:“hostName”,“stringTest”:“equal”,“stringValue”:ip}]}”
response=requests.post('https://url/api/computers/search?expand=expand,headers=headers,params=params,data=data)
打印(答复)
我得到了400个回复。。不确定语法中缺少了什么…数据需要是字典,请尝试以下操作:
数据={
“最大项”:10,
“searchCriteria”:[{“fieldName”:“主机名”,“stringTest”:“相等”,“stringValue”:ip}]
}
还可以将参数转换为字典:
params={“expand”:expand}
以及在张贴时:
response=requests.post(f'https://{url}/api/computers/search',headers=headers,params=params,data=data)
###或者你也可以
response=requests.post(f'https://{url}/api/computers/search?expand={expand}',headers=headers,data=data)
Hi我仍然得到400,你能确认我的expand=computerStatus
和response=requests.post('https://url/api/computers/search?expand=expand“,headers=headers,params=params,data=data)
语法正确吗?尤其是expand=expand在上面的urloh右边,如果要在字符串中对其进行编码,这里不需要参数,基本上使用f字符串(假设您使用的是python 3.6+或格式化字符串),requests.post(f'https://url/api/computers/search?expand={expand}“,headers=headers,data=data
我将根据需要编辑我的答案谢谢,仍然得到400分,这次我直接包含了url而不是替换,但结果是一样的……shell脚本工作正常……小古怪很可能意味着主机名无法解析。就像上面所说的。如果不使用url/api,可以尝试使用localhost:8000/api或127.0.0.1:8000/api假设端点托管在您的计算机上,端口为8000。哦,可能您正试图将url作为字符串写入,在这种情况下,请使用类似于我用于{expand}的f字符串,即:response=requests.post(f'https://{url}/api/computers/search?expand={expand}',headers=headers,data=data)
我重新编辑了我的答案,这似乎是我现在遇到的网络问题requests.exceptions.ConnectionError:HTTPSConnectionPool(host='https',port=443):url超过了最大重试次数:/xxx.xx..x..com/api/computers/search?expand=computerStatus(由NewConnectionError引起)(':未能建立新连接:[Errno 11001]getaddrinfo失败了)
…但是我想知道为什么在linux机器上运行它会成功,linux脚本..嗯..谢谢你的耐心在这方面帮助我