简体   繁体   中英

Converting curl into pycurl format using python3

I am using python3 pycurl module to send post request. I have a curl request of this format..

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

i am doing something like this with 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()

But its not working.Can yomebody please help where i am doing wrong? thanks alot

Curl has an option to transform curl commands to libcurl code, use --libcurl option followed by output file name to hold the generated code “code.txt” in our example below,

Note the code in c but it is not hard to read and transform it to python code

Curl --libcurl code.txt theRestOfyourCommand

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM