On Thu, Jun 19, 2014 at 9:51 PM, Robin Becker <ro...@reportlab.com> wrote:
>> Since you mention urllib2, I'm assuming this is Python 2.x, not 3.x.
>> The exact version may be significant.
> I can use python >= 3.3 if required.
The main reason I ask is in case something's changed. Basically, what
I did was go to my Python 2 installation (which happens to be 2.7.3,
because that's what Debian Wheezy ships with - not sure why it hasn't
been updated beyond that), pull up urllib2.py, and step through
manually, seeing where the hostname gets turned into an IP address.
Hence, this code:
>> import socket.
>> orig_create_connection = socket.create_connection
>> def create_connection(address, *args, **kwargs):
>> if address == "domainA": address = "188.8.131.52"
>> return orig_create_connection(address, *args, **kwargs)
>> socket.create_connection = create_connection
>> # Proceed to use urllib2.urlopen()
>> Untested, but may do what you want.
> this seems like a way forward
So if it works, that's great! If it doesn't, and you're on a different
version of Python (2.6? 2.4 even?), you might want to look at
repeating the exercise I did, with your actual Python.
But as a general rule, I'd recommend going with Python 3.x unless you
have a good reason for using 2.x. If a feature's been added to let you
mock in a different IP address, it'll be in 3.something but probably
not in 2.7.
>> Normally, though, I'd look at just changing the hosts file, if at all
>> possible. You're right that it does change state for your whole
>> computer, but it's generally the easiest solution.
> me too, but I want to start torturing from about 10 different servers so
> plumbum + a python script seems like a good choice and I would not really
> want to hack the hosts files back and forth on a regular basis.
Fair enough. In that case, the best thing to do would probably be
monkey-patching, with code along the lines of what I posted above.
Make the change as small and as specific as you can.