python - 如何使用 Shelex 将变量传递给curl 命令

标签 python shell

我在 python 中使用下面的curl命令,并且我能够成功运行它。但我想将身份验证 ID 和租户 ID 存储在一个变量中,然后在curl 命令中使用该变量。请让我知道我该怎么做。先感谢您。

import shlex
auth_id = 'test'

tenant_id = 'testt'
cmd = '''curl -X POST -i -u auth_id@tenant:password -H 'Content-Type: 
application/json' -d '{"rotate": 23.07, "pressure": 45.85}'  
http://localhost:8080/telemetry'''
args = shlex.split(cmd)
process = subprocess.Popen(args, shell=False, 
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()

最佳答案

您可以使用 pycurl 代替:

import pycurl
import json

headers = ['Content-Type: application/json']
data = json.dumps({"rotate": 23.07, "pressure": 45.85})

credentials = 'username:password'

p = pycurl.Curl()
p.setopt(pycurl.POST, 1)
p.setopt(pycurl.HTTPHEADER, headers)
p.setopt(pycurl.POSTFIELDS, data)
p.setopt(pycurl.VERBOSE, True)
p.setopt(pycurl.USERPWD, credentials)
pass_url=str("http://localhost:8080/")
p.setopt(pycurl.URL, pass_url)
p.perform()
p.close() 

关于python - 如何使用 Shelex 将变量传递给curl 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57678012/

相关文章:

python - 在 python 2.7 中导入 urllib2 失败

python /iptables : Original Destination IP

python - 如何从 numpy ndarray 创建 numpy ndarray?

mysql - shell 脚本 : Launch Mysql script using shell variable

Linux shell : Verify whether a file exists and is a soft link?

python - 将功能分解为被动(算法)和主动(执行)对象

python - 有python的文档工具吗?

bash - 读取文件权限并在 shell 中应用于另一个文件

shell - 如何在 Shell Scripting 中获取文件的创建/修改日期?

linux - 如何使用sed将字符串替换为数字?