Last active
August 29, 2015 14:03
-
-
Save dipanjanS/6754509013f961041bfb to your computer and use it in GitHub Desktop.
Change IP address to send requests at application level
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Sat Jun 07 11:11:16 2014 | |
@author: Dipanjan | |
""" | |
#import sys | |
#from lxml import html | |
#import urllib2 | |
#import json | |
# | |
#target_url = 'http://www.google.com' | |
# | |
#def get_product_urls(target_url): | |
# | |
# request = urllib2.Request(target_url) | |
# request.add_header("User-Agent", "Google Chrome") | |
# raw_html = urllib2.build_opener().open(target_url).read() | |
# print raw_html | |
# | |
# | |
#get_product_urls(target_url) | |
import httplib | |
import socket | |
import urllib2 | |
class BindableHTTPConnection(httplib.HTTPConnection): | |
def connect(self): | |
"""Connect to the host and port specified in __init__.""" | |
self.sock = socket.socket() | |
self.sock.bind((self.source_ip, 0)) | |
if isinstance(self.timeout, float): | |
self.sock.settimeout(self.timeout) | |
self.sock.connect((self.host,self.port)) | |
def BindableHTTPConnectionFactory(source_ip): | |
def _get(host, port=None, strict=None, timeout=0): | |
bhc=BindableHTTPConnection(host, port=port, strict=strict, timeout=timeout) | |
bhc.source_ip=source_ip | |
return bhc | |
return _get | |
# replace 0.0.0.0 with allowed IP address when sending request. | |
class BindableHTTPHandler(urllib2.HTTPHandler): | |
def http_open(self, req): | |
return self.do_open(BindableHTTPConnectionFactory('0.0.0.0'), req) | |
opener = urllib2.build_opener(BindableHTTPHandler) | |
opener.open("http://icanhazip.com/").read() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment