v0mit

Untitled

Aug 29th, 2011
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. '''
  2. Created on 17. aug. 2011
  3.  
  4. @author: v0mit
  5. '''
  6. import urllib, urllib2
  7.  
  8. class _http_handler():
  9.     def __init__(self, proxy=None):
  10.        
  11.         if proxy:
  12.             self.install_proxy(proxy)
  13.        
  14.         else:
  15.             self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
  16.            
  17.         self.user_agent = 'http_handler.v1.1 [email protected]'
  18.         urllib2.install_opener(self.opener)
  19.  
  20.     def request(self, url, data=None):
  21.         req = urllib2.Request(url)
  22.         req.add_header('User-Agent', self.user_agent)
  23.        
  24.         if data != None:
  25.             data = urllib.urlencode(data)
  26.             try:
  27.                 response = self.opener.open(req,data)
  28.             except urllib2.URLError as errno:              
  29.                 raise HTTPError("[!]urllib2.URLError({0})\n[URL:{1}][Data:{2}".format(errno, url, data))
  30.                
  31.             return response.read()
  32.         else:
  33.             try:
  34.                 response = self.opener.open(req)
  35.             except urllib2.URLError as errno:
  36.                 raise HTTPError("[!]urllib2.URLError({0})\n[URL:{1}]".format(errno, url))
  37.                
  38.             except ValueError as errno:
  39.                 raise HTTPError("[!]ValueError({0}\n".format(errno))
  40.            
  41.             return response.read()
  42.    
  43.     def install_proxy(self, proxy):
  44.         if len(proxy) is not 2:
  45.             raise HTTPError("Invalid proxy.")
  46.             return
  47.            
  48.         if proxy[0] == "SOCK":
  49.             import socket
  50.            
  51.             try:
  52.                 import socks
  53.             except ImportError:
  54.                 raise HTTPError("SocksiPy not installed, http://socksipy.sourceforge.net/")
  55.                 return
  56.            
  57.             try:        
  58.                 ip, port = proxy[1].split(":")
  59.             except ValueError as errno:
  60.                 raise HTTPError("{0} Invalid proxy(IP:PORT)".format(errno))
  61.        
  62.             socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, ip, int(port))
  63.             socket.socket = socks.socksocket
  64.            
  65.             self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
  66.            
  67.         else:          
  68.             http_proxy= {"http":"http://{0}/".format(proxy[1])}
  69.             proxy_support = urllib2.ProxyHandler(http_proxy)
  70.             self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(), proxy_support)
  71.        
  72. class HTTPError(Exception):
  73.     def __init__(self, value):
  74.         self.value = value
  75.     def __str__(self):
  76.         return repr(self.value)
Advertisement
Add Comment
Please, Sign In to add comment