Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #73418

Re: urllib/urllib2 support for specifying ip address

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'exercise': 0.04; 'hosts': 0.07; 'subject:support': 0.07; '*args,': 0.09; 'assuming': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'posted': 0.15; '**kwargs)': 0.16; '**kwargs):': 0.16; 'did,': 0.16; 'enough.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'great!': 0.16; 'hostname': 0.16; 'normally,': 0.16; 'urllib2,': 0.16; 'wrote:': 0.18; 'file,': 0.19; "hasn't": 0.19; 'thu,': 0.19; 'solution.': 0.20; 'seems': 0.21; 'import': 0.22; 'hack': 0.22; 'cc:addr:python.org': 0.22; 'installation': 0.23; 'cc:2**0': 0.24; 'script': 0.25; 'mention': 0.26; 'subject:/': 0.26; 'code:': 0.26; 'required.': 0.27; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'generally': 0.29; 'message-id:@mail.gmail.com': 0.30; 'along': 0.30; "i'm": 0.30; '(which': 0.31; 'code': 0.31; 'lines': 0.31; '3.x': 0.31; 'changed.': 0.31; 'probably': 0.32; 'regular': 0.32; 'proceed': 0.33; 'actual': 0.34; 'updated': 0.34; "i'd": 0.34; 'possible.': 0.35; 'beyond': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'really': 0.36; 'changing': 0.37; 'step': 0.37; 'skip:o 20': 0.38; 'basis.': 0.38; 'easiest': 0.38; 'files': 0.38; 'pm,': 0.38; 'does': 0.39; 'though,': 0.39; 'sure': 0.39; 'address.': 0.39; 'skip:u 10': 0.60; 'skip:o 30': 0.61; "you're": 0.61; 'back': 0.62; 'address': 0.63; 'different': 0.65; 'forward': 0.65; 'address,': 0.75; 'forth': 0.81; '2.7.': 0.84; 'mock': 0.84; 'ships': 0.84; 'that),': 0.91; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=c/MXid2Cm69974mUbFTXAtgFnpWk7WukTR5cgk6uu88=; b=iRiycZwqp73VwHFZoUxzYV6Nh/rvZkt6/F5lU+bkmh4vifJjJCnRwfokvKwgUXorZW fH7k3X5b4wOR9lblhCiZAK6qiAvWFyGxdbdL4co7a8RKotBWrJGBtX8jiAiHF1HKcW34 G31aSf4tkAZ0elPulrqwWjDfYYTLt5wy+7b89O6QZNuf7xwK/+JFaNxaYLd1coGHm3+p GpMJMgb6mZehZL7Ep0zUE3BuZr9tW1KHZfbO3XS16/aio5s/Xlypv9pqagqqRnhQVik+ VLQXiPmD78cU8xAonJa2he4hjZKLn8chzRA5f9PZC2CL44YfQRwosqwnbzwbNVzXJ694 C2jQ==
MIME-Version 1.0
X-Received by 10.220.103.141 with SMTP id k13mr3574697vco.25.1403179412033; Thu, 19 Jun 2014 05:03:32 -0700 (PDT)
In-Reply-To <53A2CECB.6060605@chamonix.reportlab.co.uk>
References <53A2ABBB.3080907@chamonix.reportlab.co.uk> <CAPTjJmosrF1pKKEiLdPg_XOwmZMNMtgH13YipSxJWg-VZ9kAkg@mail.gmail.com> <53A2CECB.6060605@chamonix.reportlab.co.uk>
Date Thu, 19 Jun 2014 22:03:31 +1000
Subject Re: urllib/urllib2 support for specifying ip address
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.11146.1403179413.18130.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1403179413 news.xs4all.nl 2977 [2001:888:2000:d::a6]:35936
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:73418

Show key headers only | View raw


On Thu, Jun 19, 2014 at 9:51 PM, Robin Becker <robin@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 = "1.2.3.4"
>>      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.
>>
>> ChrisA
>>
> 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.

ChrisA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: urllib/urllib2 support for specifying ip address Chris Angelico <rosuav@gmail.com> - 2014-06-19 22:03 +1000

csiph-web