2

我不知道如何使用 pycurl 或 requests 在 python 中编写以下 curl 命令。

curl -X DELETE -d '{"key": "value"}' http://url/json
4

2 回答 2

0

基于来自requestsquickstart的示例:

import json

url = 'http://url/json'
payload = {'key': 'value'}
headers = {'Accept': 'application/json', 'Content-Type': 'application/json'}

r = requests.delete(url, data=json.dumps(payload), headers=headers)
print(r.json())

curl 命令的更直译是:

import os
import sys
import requests

headers={"Content-Type": "application/x-www-form-urlencoded"}
r = requests.delete("http://url/json", data=b'{"key": "value"}',
                    headers=headers)
n = os.write(sys.stdout.fileno(), r.content) # support bytes in Python 2 & 3 
于 2013-06-13T02:44:08.577 回答
0
  • 你可以看看:

来自 libcurl 文档的 CURLOPT_CUSTOMREQUEST

示例我曾经将“DELE file.txt”发送到 FTP 服务器。

def delete_ftp_hash_file(self, ftp_hash_file_old):
    c = pycurl.Curl()
    delete_hash_file = 'DELE ' + ftp_hash_file_old
    sys.stderr.write("{0:.<20} : {1} \n".format('FTP command ', delete_hash_file))
    c.setopt(pycurl.URL, self.server)
    c.setopt(pycurl.USERNAME, self.username)
    c.setopt(pycurl.PASSWORD, self.password)
    c.setopt(pycurl.VERBOSE, True)
    c.setopt(pycurl.DEBUGFUNCTION, self.test)
    c.setopt(pycurl.CUSTOMREQUEST, delete_hash_file)
    try:
        c.perform()
    except Exception as e:
        print e
    c.close()

def test(self, debug_type, debug_msg):
    if len(debug_msg) < 300:
        print "debug(%d): %s" % (debug_type, debug_msg.strip())
于 2013-10-30T22:05:33.037 回答