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


Groups > comp.lang.python > #73416

Re: urllib/urllib2 support for specifying ip address

From Robin Becker <robin@reportlab.com>
Subject Re: urllib/urllib2 support for specifying ip address
Date 2014-06-19 12:51 +0100
References <53A2ABBB.3080907@chamonix.reportlab.co.uk> <CAPTjJmosrF1pKKEiLdPg_XOwmZMNMtgH13YipSxJWg-VZ9kAkg@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.11144.1403178715.18130.python-list@python.org> (permalink)

Show all headers | View raw


..........
>
> 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.


> Can you simply query the server by IP address rather than host name?
> According to the docs, urllib2.urlopen() doesn't check the
> certificate, so it should be accepted. Or does the server insist on
> the hostname being correct?
>
> Failing that, you could monkey-patch socket.create_connection, which
> seems to be the thing that ultimately does the work. Something like
> this:
>
> 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


> 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.
-- 
Robin Becker

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


Thread

Re: urllib/urllib2 support for specifying ip address Robin Becker <robin@reportlab.com> - 2014-06-19 12:51 +0100

csiph-web