def make_request(url, postdata='', proxy_handlers=None): handlers = [ urllib2.HTTPHandler(debuglevel=100), urllib2.HTTPSHandler(debuglevel=100) ] if proxy_handlers: handlers.extend(proxy_handlers) opener = urllib2.build_opener(*handlers) url_req = urllib2.Request(url, postdata) handle = None try: handle = opener.open(url_req) print '****************************** HEADERS ******************************' print handle.info() print '****************************** CONTENT ******************************' print handle.read() except urllib2.HTTPError as e: print 'Error code:', e.code, 'Exception:', str(e) except urllib2.URLError as e: code, errstr = e.reason print 'Error code:', code, 'Exception:', errstr except Exception as e: print 'Exception:', str(e) finally: if handle: handle.close() handle = None
def make_request(url, postdata='', proxy_handlers=None): handlers = [urllib2.HTTPHandler(debuglevel=100), urllib2.HTTPSHandler(debuglevel=100)] if proxy_handlers: handlers.extend(proxy_handlers) opener = urllib2.build_opener(*handlers) url_req = urllib2.Request(url, postdata) handle = None try: handle = opener.open(url_req) print '****************************** HEADERS ******************************' print handle.info() print '****************************** CONTENT ******************************' print handle.read() except urllib2.HTTPError as e: print 'Error code:', e.code, 'Exception:', str(e) except urllib2.URLError as e: code, errstr = e.reason print 'Error code:', code, 'Exception:', errstr except Exception as e: print 'Exception:', str(e) finally: if handle: handle.close() handle = None
def build_connect_opener(cert=None, proxy_handlers=None, auth_handlers=None): """Builds the list of opener objects required for the specific type of request.""" handlers = [HTTPSConnectHandler(cert=cert)] if proxy_handlers: handlers.extend(proxy_handlers) if auth_handlers: handlers.extend(auth_handlers) return urllib2.build_opener(*handlers)
def build_opener(cert=None, proxy_handlers=None, auth_handlers=None, keepalive=False): """Builds the list of opener objects required for the specific type of request.""" handlers = [urllib2.HTTPSHandler(tcp_keepalive=keepalive, cert=cert)] if proxy_handlers: handlers.extend(proxy_handlers) if auth_handlers: handlers.extend(auth_handlers) return urllib2.build_opener(*handlers)
def main(): parser = OptionParser(usage='Usage: %prog [options] (-h for help)') parser.add_option('-p', '--port', dest='port', default='8088') options, args = parser.parse_args() proxy_server = 'http://localhost:%s' % options.port proxy_handler = urllib2.ProxyHandler({ 'http': proxy_server, 'https': proxy_server, }) proxy = ProxyServer(int(options.port)) proxy.start() headers = None timeout_sec = 5 for url, postdata in THE_INTERWEBS: print '****************' url_req = urllib2.Request(url, postdata) if headers: for item, val in headers: url_req.add_header(item, val) handlers = [proxy_handler] url_opener = urllib2.build_opener(*handlers) handle = None if timeout_sec: handle = url_opener.open(url_req, timeout=timeout_sec) print handle.info().items() print handle.read() print '****************' proxy.stop() return 0