Advertisement
gauravssnl

openAnything

Sep 28th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. # openAnything.py
  2. #opens url,file,strings
  3. import urllib2,urlparse
  4. from StringIO import StringIO
  5.  
  6. USER_AGENT= "OpenAnythhing/1.0"
  7.  
  8. class SmartRedirectHandler(urllib2.HTTPRedirectHandler):
  9.     def http_error_301(self,req,fp,code,msg,headers):
  10.         result=urllib2.HTTPRedirectHandler.http_error_301(self,req,fp,code,msg,headers)
  11.         result.status=code
  12.         return result
  13.     def http_error_302(self,req,fp,code,msg,headers):
  14.         result=urllib2.HTTPRedirectHandler.http_error_302(self,req,fp,code,msg,headers)
  15.         result.status=code
  16.         return result
  17.  
  18. class DefaultErrorHandler(urllib2.HTTPDefaultErrorHandler):
  19.     def http_error_default(self,req,fp,code,msg,headers):
  20.         result=urllib2.HTTPError(req.get_full_url(),code,msg,headers,fp)
  21.         result.status=code
  22.         return result
  23.        
  24. def openAnything(source,etag=None,lastmodified=None,agent=USER_AGENT):
  25.     if hasattr(source,"read"):
  26.         return source
  27.     if source == "-":
  28.         return sys.stdin
  29.     if urlparse.urlparse(source)[0]== "http":
  30.         # open url with urllib2
  31.         request=urllib2.Request(source)
  32.         request.add_header("User-Agent",agent)
  33.         if etag:
  34.             request.add_header("If-None-Match",etag)
  35.         if lastmodified:
  36.             request.add_header("If-Modified-Since",lastmodified)
  37.         request.add_header("Accept-Encoding","gzip")    
  38.         opener=urllib2.build_opener(SmartRedirectHandler(),DefaultErrorHandler())
  39.         return opener.open(request)
  40.     # try to open with native open function(if source is a filename)
  41.     try:
  42.         return open(source)
  43.     except (IOError,OSError):
  44.         pass        
  45.     # treat source as string
  46.     return StringIO(str(source))
  47.  
  48. def fetch(source,etag=None,last_modified=None,agent=USER_AGENT):
  49.     """Fetch data and metadata from a URL,file,stream,or string"""
  50.     result={}
  51.     f=openAnything(source,etag,last_modified,agent)    
  52.     result["data"]=f.read()
  53.     if hasattr(f,"headers"):
  54.         # save ETag if the server sent one
  55.         result["etag"]=f.headers.get("ETag")
  56.         # save lastmodified headers if server sent one
  57.         result["lastmodified"]=f.headers.get("Last-Modified")
  58.     if hasattr(f,"url"):
  59.         result["url"]=f.url
  60.         result["status"]=200
  61.     if hasattr(f,"status"):
  62.         result["status"]=f.status
  63.     f.close()
  64.    
  65.     return result
  66.  
  67. # test the script
  68. print fetch("http://ics.bsbportal.com/")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement