pycurl使用socks5代理功能

pycurl使用socks5代理功能

如何在pycurl使用socks5代理,在官方手册中并没有查到,这里根据python的习惯性写法,假想了一下没想到成功

贴一个代码

import pycurl
import StringIO
import sys
from termcolor import colored

url="about:blank"
cookiefile ="/tmp/test.cookie"
for cmd in sys.argv:
    cmd_info=cmd.split("=")
    if cmd_info[0].upper() == "URL":
        url=cmd_info[1]
    if cmd_info[0].upper() == "COOKIE":
        cookiefile=cmd_info[1]

if url == "about:blank":
   print("Help : %s %s"%(sys.argv[0], "url=http://www.google.com"))
   sys.exit()

proxy ="%s://%s:%s"%("socks5","192.168.1.253","5080")

try:
   curl=pycurl.Curl()
   curl.setopt(pycurl.URL,url)
   curl.setopt(pycurl.MAXREDIRS, 5)
   curl.setopt(pycurl.FOLLOWLOCATION,1)
   curl.fp=StringIO.StringIO()
   curl.setopt(curl.WRITEFUNCTION,curl.fp.write)
   curl.setopt(curl.USERAGENT,"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)")
   curl.setopt(pycurl.COOKIEFILE,cookiefile)
   curl.setopt(pycurl.COOKIEJAR,cookiefile)
   curl.setopt(pycurl.CONNECTTIMEOUT,30) #Connect time out
   curl.setopt(pycurl.TIMEOUT,600) #Download time out
   curl.setopt(pycurl.SSL_VERIFYPEER,False)
   curl.setopt(pycurl.SSL_VERIFYHOST,False)
   curl.setopt(pycurl.PROXY,proxy)
   curl.setopt(pycurl.REFERER,url)
   curl.perform()
   print curl.getinfo(curl.HTTP_CODE)
   print curl.fp.getvalue()
except Exception,error_info:
   print "[Error] curl => %s"%(error_info)

curl.fp.close()
curl.close()

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

14 + 3 =