pycurl的一些example
直接在github上找
https://github.com/pycurl/pycurl/tree/master/examples
basicfirst.py:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vi:ts=4:et
import sys
import pycurl
PY3 = sys.version_info[0] > 2
class Test:
def __init__(self):
self.contents = ''
if PY3:
self.contents = self.contents.encode('ascii')
def body_callback(self, buf):
self.contents = self.contents + buf
sys.stderr.write("Testing %s\n" % pycurl.version)
t = Test()
c = pycurl.Curl()
c.setopt(c.URL, 'https://curl.haxx.se/dev/')
c.setopt(c.WRITEFUNCTION, t.body_callback)
c.perform()
c.close()
print(t.contents)
xmlrpc_curl.py:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vi:ts=4:et
# We should ignore SIGPIPE when using pycurl.NOSIGNAL - see
# the libcurl tutorial for more info.
try:
import signal
signal.signal(signal.SIGPIPE, signal.SIG_IGN)
except ImportError:
pass
try:
from cStringIO import StringIO
except ImportError:
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
try:
import xmlrpclib
except ImportError:
import xmlrpc.client as xmlrpclib
import pycurl
import sys
PY3 = sys.version_info[0] > 2
class CURLTransport(xmlrpclib.Transport):
"""Handles a cURL HTTP transaction to an XML-RPC server."""
xmlrpc_h = [ "Content-Type: text/xml" ]
def __init__(self, username=None, password=None):
self.c = pycurl.Curl()
self.c.setopt(pycurl.POST, 1)
self.c.setopt(pycurl.NOSIGNAL, 1)
self.c.setopt(pycurl.CONNECTTIMEOUT, 30)
self.c.setopt(pycurl.HTTPHEADER, self.xmlrpc_h)
if username != None and password != None:
self.c.setopt(pycurl.USERPWD, '%s:%s' % (username, password))
self._use_datetime = False
def request(self, host, handler, request_body, verbose=0):
b = StringIO()
self.c.setopt(pycurl.URL, 'http://%s%s' % (host, handler))
self.c.setopt(pycurl.POSTFIELDS, request_body)
self.c.setopt(pycurl.WRITEFUNCTION, b.write)
self.c.setopt(pycurl.VERBOSE, verbose)
self.verbose = verbose
try:
self.c.perform()
except pycurl.error:
v = sys.exc_info()[1]
if PY3:
v = v.args
raise xmlrpclib.ProtocolError(
host + handler,
v[0], v[1], None
)
b.seek(0)
return self.parse_response(b)
if __name__ == "__main__":
## Test
server = xmlrpclib.ServerProxy("http://betty.userland.com",
transport=CURLTransport())
print(server)
try:
print(server.examples.getStateName(41))
except xmlrpclib.Error:
v = sys.exc_info()[1]
print("ERROR", v)
D:\python\pycurl-master\tests里的post_test.py有一段可以参考:
def check_post(self, send, expect, endpoint):
self.curl.setopt(pycurl.URL, endpoint)
self.curl.setopt(pycurl.HTTPPOST, send)
#self.curl.setopt(pycurl.VERBOSE, 1)
sio = util.BytesIO()
self.curl.setopt(pycurl.WRITEFUNCTION, sio.write)
self.curl.perform()
self.assertEqual(200, self.curl.getinfo(pycurl.HTTP_CODE))
body = sio.getvalue().decode()
returned_fields = json.loads(body)
self.assertEqual(expect, returned_fields)
网上有一个利用pycurl上传文件的例子,胜在简洁,因此记录下来:
# -*- coding: utf-8 -*-
import pycurl
import os
def upload(filename):
pc = pycurl.Curl()
pc.setopt(pycurl.POST, 1)
pc.setopt(pycurl.URL, 'your url')
pc.setopt(pycurl.HTTPPOST, [('file fieldname', (pc.FORM_FILE, filename)),\
('filed1_name', (pc.FORM_CONTENTS, "value"))])
pc.perform()
response_code = pc.getinfo(pycurl.RESPONSE_CODE)
pc.close()
if __name__ == "__main__":
filelist = os.listdir(".")
for f in filelist:
upload(f)
print "OK"