0

i am using urllib2 to make a http request using a proxy server, how can i use source interface ipaddress while making the request similar to curl

curl --interface xx.xx.xx.xx -x yy.yy.yy.yy:8080 www.google.com

#!/usr/bin/env python

import urllib2

proxy = urllib2.ProxyHandler({'http': 'xx.xx.xx.xx:8080'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
result = urllib2.urlopen('http://www.google.com')

for line in result:
    print line.rstrip("\n")
krisdigitx
  • 7,068
  • 20
  • 61
  • 97
  • possible duplicate of [Source interface with Python and urllib2](http://stackoverflow.com/questions/1150332/source-interface-with-python-and-urllib2) – mechanical_meat Apr 05 '12 at 16:34

1 Answers1

0

There is a similar answer with a question which may help you. There's no need to paste the answer here, but basically the API doesn't give you any hook, so you'll have to deep into the API stack and either modify the sources or monkey patch the code

import socket
true_socket = socket.socket
def bound_socket(*a, **k):
    sock = true_socket(*a, **k)
    sock.bind((sourceIP, 0))
    return sock
socket.socket = bound_socket 

I hope I have understood your question.

Community
  • 1
  • 1
Raffaele
  • 20,627
  • 6
  • 47
  • 86