使用python3将curl转换为pycurl格式

时间:2019-02-22 16:02:44

标签: python-3.x http curl pycurl

我正在使用python3 pycurl模块发送发布请求。 我有这种格式的卷曲请求。.

curl -X POST
 --data '{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{
 "from": "0xdd1b1f8a644be8e1f41fbe6d7db25b56301ac6a2",
 "to": "0x90299471062a53cc9e675b273901baa65e641fad",
 "data":"0xf6b7280400000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001"}],
 "id":1}' 
 -H "Content-Type: application/json"  http://127.0.0.1:5436

我正在用pycurl做类似的事情。

import pycurl
import json

my_url = 'http://127.0.0.1:5436'
data = json.dumps({"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{"from": "0xdd1b1f8a644be8e1f41fbe6d7db25b56301ac6a2","to": "0x90299471062a53cc9e675b273901baa65e641fad","data": "0xf6b7280400000000000000000000000000000000000000000000000000000000000000440000000000000000000000000000000000000000000000000000000000000001"}],"id":1})

c = pycurl.Curl()
c.setopt(pycurl.URL, my_url)
c.setopt(pycurl.HTTPHEADER, ['Accept: application/json'])
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, data)
c.perform()

但是它不起作用。有人可以帮我解决我做错的地方吗?非常感谢

1 个答案:

答案 0 :(得分:0)

Curl具有将curl命令转换为libcurl代码的选项,在下面的示例中,使用--libcurl选项和输出文件名来保存生成的代码“ code.txt”,

请注意c中的代码,但不难阅读并将其转换为python代码

Curl --libcurl code.txt theRestOfyourCommand