Skip to content

Instantly share code, notes, and snippets.

@eskil
Created April 4, 2012 00:48
Show Gist options
  • Save eskil/2296775 to your computer and use it in GitHub Desktop.
Save eskil/2296775 to your computer and use it in GitHub Desktop.
Example code doing SSL using pycurl and CurlMulti
# Example code doing SSL using pycurl and CurlMulti
import pycurl
import cStringIO
response_buffer = cStringIO.StringIO()
multi = pycurl.CurlMulti()
curl = pycurl.Curl()
curl.setopt(pycurl.URL, 'https://www.yammer.com')
curl.setopt(pycurl.SSL_VERIFYPEER, 1)
curl.setopt(pycurl.SSL_VERIFYHOST, 2)
curl.setopt(pycurl.WRITEFUNCTION, response_buffer.write)
curls = [curl]
for curl in curls:
multi.add_handle(curl)
num_handles = len(curls)
while num_handles:
# Do curlmulti perform until all writing is done.
while True:
ret, num_handles = multi.perform()
print 'multi perform =', ret, num_handles
if ret != pycurl.E_CALL_MULTI_PERFORM:
break
# Wait 1s...
ret = multi.select(1)
print 'multi select =', ret
while True:
# Check and handle reading
num_q, ok_list, err_list = multi.info_read()
print 'multi read =', num_q, ok_list, err_list
for curl in ok_list:
code = curl.getinfo(pycurl.HTTP_CODE)
print 'code', code
print 'response', response_buffer.getvalue()
multi.remove_handle(curl)
for handle in err_list:
print 'fail...'
multi.remove_handle(curl)
if num_q == 0:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment