pycurl简单用法

在项目中使用到的一个方法

 1     def postToGoogle(self, postData):
 2         cookieFile = filepath
 3         html = StringIO.StringIO()
 4         c = pycurl.Curl()
 5         c.fp = html
 6         c.setopt(pycurl.URL, loginAction_url) #目标url
 7         c.setopt(pycurl.WRITEFUNCTION, html.write)
 8         c.setopt(pycurl.FOLLOWLOCATION, 1)
 9         c.setopt(pycurl.REFERER, loginUrl)  #前一级url
10         c.setopt(pycurl.MAXREDIRS, 5)
11         c.setopt(pycurl.SSL_VERIFYPEER, 0)
12         c.setopt(pycurl.POSTFIELDS, postData)
13         c.setopt(pycurl.POST, 1)
14         c.setopt(pycurl.PROXY, '127.0.0.1:8087') #代理设置
15         c.setopt(pycurl.COOKIEFILE, cookieFile)
16         c.setopt(pycurl.COOKIEJAR, cookieFile)
17         c.setopt(pycurl.USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.57 Safari/534.24')
18         c.perform()
19         httpCode = c.getinfo(pycurl.HTTP_CODE)
20         cont = c.fp.getvalue()
21         if httpCode != 200: raise Exception("can not get page content")
22         return cont
posted @ 2012-12-03 11:01  践道者  阅读(713)  评论(0编辑  收藏  举报