gpt4 book ai didi

Python HTTP请求替换bash的curl

转载 作者:行者123 更新时间:2023-12-01 03:54:31 24 4
gpt4 key购买 nike

我正在将一些自动化脚本从bash移植到python,它们几乎都是以下格式的curl命令:

curl -k -H "Content-Type: application/json" -X POST -d '{               "Request": {
"MessageID": "xxxx",
"MessageDateTime": "xxxxx",
"SourceSystem": "xxxx",

}
}' https://myUrl.xxx

在 Python 中准确构建此结构的最佳方法是什么?到目前为止我已经:

import requests

headers = {'Content-Type': 'application/json'}
payload = {'All the data'}
conn = httplib.HTTPConnection("myUrl.xxx")
conn.request("POST", "", payload, headers)
response = conn.getresponse()
print response

我想确保 -k、-d 和 -x bash 选项反射(reflect)在此脚本中。谢谢!

最佳答案

您可以直接使用requests.post-k 对应于verify=False:

from datetime import datetime as DateTime
import requests
import json

URL = "https://myUrl.xxx"

message = {
"Request": {
"MessageID": "xxxx",
"MessageDateTime": DateTime.now().isoformat(),
"SourceSystem": "xxxx",
}
}

response = requests.post(URL, data=json.dumps(message), verify=False, headers={"Content-Type":"application/json"})
data = response.json()

关于Python HTTP请求替换bash的curl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37731368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com