将 Bash cURL 命令翻译成 Python
Translating Bash cURL commands to Python
我有以下工作在 Bash。这将如何使用请求或 pycurl 在 Python 中编写?
curl -o .output_data.xml -v --cert ../cert/my_cert.pem:password123 -k 'https://my.target.url' -H 'Content-Type: application/json' -d '{"document":{"header":{"exportType":"XML"}}}'
您可以使用:
bashCommand = "curl -o .output_data.xml -L -X GET -v --cert ../cert/my_cert.pem:password123 -k 'https://my.target.url' -H 'Content-Type: application/json' -d '{"document":{"header":{"exportType":"XML"}}}'"
import subprocess
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
感谢 Tripleee 关于
https://github.com/psf/requests/issues/1573#issuecomment-169916326 下面的代码片段现在可以使用了:
我只需要正确映射 cURL 选项参数:
--cert maps to c.setopt(pycurl.SSLCERT, cert_file_path) and c.setopt(pycurl.SSLKEYPASSWD, "password123")
-k maps to c.setopt(pycurl.SSL_VERIFYPEER, False)
-H maps to c.setopt(pycurl.HTTPHEADER,["Content-Type: application/json"])
-d maps to c.setopt(pycurl.POSTFIELDS, json.dumps(params))
-o
没有映射,所以我使用缓冲区 c.setopt(b.WRITEFUNCTION, b.write)
捕获输出。
b.getvalue()
将允许我稍后使用 Element Tree 或类似方法从捕获的 XML 字符串中解析数据。
import pycurl
import io
import json
cert_file_path = "../cert/my_cert.pem"
url = "https://my.target.url"
params={"document":{"header":{"exportType":"XML"}}}
b = io.BytesIO()
c = pycurl.Curl()
c.setopt(pycurl.URL, url)
c.setopt(pycurl.SSLCERT, cert_file_path)
c.setopt(pycurl.SSLKEYPASSWD, "password123")
c.setopt(pycurl.SSL_VERIFYPEER, False)
c.setopt(pycurl.HTTPHEADER,["Content-Type: application/json"])
c.setopt(pycurl.POSTFIELDS, json.dumps(params))
c.setopt(c.WRITEFUNCTION, b.write)
c.perform()
xml_string = b.getvalue().decode('UTF-8')
我有以下工作在 Bash。这将如何使用请求或 pycurl 在 Python 中编写?
curl -o .output_data.xml -v --cert ../cert/my_cert.pem:password123 -k 'https://my.target.url' -H 'Content-Type: application/json' -d '{"document":{"header":{"exportType":"XML"}}}'
您可以使用:
bashCommand = "curl -o .output_data.xml -L -X GET -v --cert ../cert/my_cert.pem:password123 -k 'https://my.target.url' -H 'Content-Type: application/json' -d '{"document":{"header":{"exportType":"XML"}}}'"
import subprocess
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
感谢 Tripleee 关于 https://github.com/psf/requests/issues/1573#issuecomment-169916326 下面的代码片段现在可以使用了:
我只需要正确映射 cURL 选项参数:
--cert maps to c.setopt(pycurl.SSLCERT, cert_file_path) and c.setopt(pycurl.SSLKEYPASSWD, "password123")
-k maps to c.setopt(pycurl.SSL_VERIFYPEER, False)
-H maps to c.setopt(pycurl.HTTPHEADER,["Content-Type: application/json"])
-d maps to c.setopt(pycurl.POSTFIELDS, json.dumps(params))
-o
没有映射,所以我使用缓冲区 c.setopt(b.WRITEFUNCTION, b.write)
捕获输出。
b.getvalue()
将允许我稍后使用 Element Tree 或类似方法从捕获的 XML 字符串中解析数据。
import pycurl
import io
import json
cert_file_path = "../cert/my_cert.pem"
url = "https://my.target.url"
params={"document":{"header":{"exportType":"XML"}}}
b = io.BytesIO()
c = pycurl.Curl()
c.setopt(pycurl.URL, url)
c.setopt(pycurl.SSLCERT, cert_file_path)
c.setopt(pycurl.SSLKEYPASSWD, "password123")
c.setopt(pycurl.SSL_VERIFYPEER, False)
c.setopt(pycurl.HTTPHEADER,["Content-Type: application/json"])
c.setopt(pycurl.POSTFIELDS, json.dumps(params))
c.setopt(c.WRITEFUNCTION, b.write)
c.perform()
xml_string = b.getvalue().decode('UTF-8')