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


Groups > comp.lang.python > #73409 > unrolled thread

Re: urllib/urllib2 support for specifying ip address

Started byChris Angelico <rosuav@gmail.com>
First post2014-06-19 19:44 +1000
Last post2014-06-19 19:44 +1000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#73409 — Re: urllib/urllib2 support for specifying ip address

FromChris Angelico <rosuav@gmail.com>
Date2014-06-19 19:44 +1000
SubjectRe: urllib/urllib2 support for specifying ip address
Message-ID<mailman.11138.1403171064.18130.python-list@python.org>
On Thu, Jun 19, 2014 at 7:22 PM, Robin Becker <robin@reportlab.com> wrote:
> I want to run torture tests against an https server on domain A; I have
> configured apache on the server to respond to a specific hostname ipaddress.
>
> I don't want to torture the live server so I have set up an alternate
> instance on a different ip address.

Since you mention urllib2, I'm assuming this is Python 2.x, not 3.x.
The exact version may be significant.

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.

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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web