Python Intro - from curl to Pycurl

 

 

curl -X "DELETE" http://www.url.com/page

 

curl -X POST "http://api.postmarkapp.com/email" \

-H "Accept: application/json" \

-H "Content-Type: application/json" \

-H "X-Postmark-Server-Token: ed742D75-5a45-49b6-a0a1-5b9ec3dc9e5d" \

-v \

-d "{From: 'sender@example.com', To: 'receiver@example.com', Subject: 'Postmark test', HtmlBody: '<html><body><strong>Hello</strong> dear Postmark user.</body></html>'}"





import pycurl, json

github_url = 'https://api.postmarkapp.com/email'

data = json.dumps({"From": "user@example.com", "To": "receiver@example.com", "Subject": "Pycurl", "TextBody": "Some text"})

c = pycurl.Curl()
c.setopt(pycurl.URL, github_url)
c.setopt(pycurl.HTTPHEADER, ['X-Postmark-Server-Token: API_TOKEN_HERE','Accept: application/json'])
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, data)
c.perform()



===================================================================================



curl -i http://localhost/rest/v1.0/tasks/3

 

mport 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())

-----------------------------------------------------------------------------
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 



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())
 

posted on 2017-08-21 01:38  fanbird2008  阅读(62)  评论(0)    收藏  举报

导航