Programming

26 Feb 2015

Proper Python pycurl example

Preston Garrison 0 Comments

Here is a good example how to call pycurl.   It expects the proxy to be in format socks://ip:port, and will default to port 8888 if you just pass it the proxy hostname.  

 

import cStringIO
import pycurl
import sys,traceback 
def GetPage(url, proxy=None):

  if proxy:
    port = 8888
    proxy = proxy.replace("socks://", "")
    if ":" in proxy:
      port = int(proxy.rsplit(":", 1)[1])
      proxy = proxy.rsplit(":", 1)[0]

  try: 
    buf = cStringIO.StringIO()
    c = pycurl.Curl()
    c.setopt(c.URL, url)
    c.setopt(c.WRITEFUNCTION, buf.write)
    c.setopt(c.CONNECTTIMEOUT, 5)
    c.setopt(c.TIMEOUT, 8)
    if proxy:
      c.setopt(pycurl.PROXY, proxy)
      c.setopt(pycurl.PROXYPORT, port)
      c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5)
    c.setopt(c.FOLLOWLOCATION, True)
    c.perform()
    c.close()
    results = buf.getvalue()
    buf.close()

  except:
    traceback.print_exc(file=sys.stdout)
    results = ""



  return results

print GetPage("http://www.whatismyip.com","socks://127.0.0.1:8888")

– 

Preston Garrison