Skip to content

Instantly share code, notes, and snippets.

@likewhoa
Forked from newbiethetest/pycurl.py
Created January 7, 2014 05:45
Show Gist options
  • Save likewhoa/8295121 to your computer and use it in GitHub Desktop.
Save likewhoa/8295121 to your computer and use it in GitHub Desktop.
import pycurl
import StringIO
import re
def initCurl():
c=pycurl.Curl()
#c.setopt(pycurl.COOKIEFILE,"cookie_file_name")
#c.setopt(pycurl.COOKIEJAR,"cookie_file_name")
c.setopt(pycurl.FOLLOWLOCATION,0)
c.setopt(pycurl.CONNECTTIMEOUT, 1) #
c.setopt(pycurl.MAXREDIRS,1)
c.setopt(pycurl.NOSIGNAL, 1)
#c.setopt(pycurl.HEADERFUNCTION, header_callback)
# c.setopt(pycurl.PROXY,'http://www.x.x.x.com:8080')
# c.setopt(pycurl.PROXYUSERPWD,'aaa:bbb')
return c
def headerCallback(buf):
match=re.search(r"Server\:\s+(.*)",buf)
if match:
return match.group(1)
def GetData(curl,url,code=True):
try:
head=['Accept:*/*',
'User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11'
]
buf=StringIO.StringIO()
curl.setopt(pycurl.WRITEFUNCTION,buf.write)
curl.setopt(pycurl.URL,url)
curl.setopt(pycurl.HTTPHEADER,head)
curl.perform()
the_page=buf.getvalue()
if code==True:
return curl.getinfo(pycurl.HTTP_CODE)
elif code=='head':
curl.setopt(pycurl.HEADERFUNCTION, headerCallback)#here!! i want to get the server information :iis or apache ,but #not work,
else:
return the_page
buf.close()
except pycurl.error,e:
return 'site cannot visit'
#another file Named test.py :
from curl import initCurl,GetData
c=initCurl()
website="http://www.google.com"
head=GetData(c,website,code='head')
print head
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment