2
我不知道如何使用pycurl或請求在python中編寫下面的curl命令。基於the example from requests
quickstart如何在Pycurl中發送DELETE請求?
curl -X DELETE -d '{"key": "value"}' http://url/json
我不知道如何使用pycurl或請求在python中編寫下面的curl命令。基於the example from requests
quickstart如何在Pycurl中發送DELETE請求?
curl -X DELETE -d '{"key": "value"}' http://url/json
:
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
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())