我不知道如何使用pycurl或者请求在python中编写以下curl命令。
curl -X DELETE -d '{"key": "value"}' http://url/json
答案 0 :(得分:2)
基于the example from requests
quickstart:
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
答案 1 :(得分:0)
CURLOPT_CUSTOMREQUEST from libcurl documentation
示例我曾经将'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())