Python HTTP请求替换bash curl [英] Python HTTP Request to replace bash curl

查看:130
本文介绍了Python HTTP请求替换bash curl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm porting some automation scripts from bash to python, and they're almost ll curl commands of the following format:

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

           }
}' https://myUrl.xxx

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

What's the best way to structure this in Python accurately? So far I have:

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选项在该脚本中得到反映。谢谢!

I want to make sure the -k, -d, and -x bash options are being reflected in this script. Thank you!

推荐答案

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

You can use requests.post directly. -k corresponds to 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆