views:

58

answers:

2

I need to get requested host's ip address using urllib2 like

import urllib2

req = urllib2.Request('http://www.example.com/')

r = urllib2.urlopen(req)

Is there any issues like ip = urllib2.gethostbyname(req)?

Sultan

+1  A: 

There's a socket.gethostbyname function which will resolve the host names if that's what you mean.

Although if you already have a connection made by urllib2, then get the destination host via your_request.get_host().

viraptor
You don't. First you create the request with the opener you have, then call `get_host` on that request object. Until you're actually connected, you cannot be sure which ip will the name resolve to.
viraptor
get_host method just returns a link not IP seems I'll have to look around for other issues. I was wondering if there is any method to get remote website ip address using proxy?
sultan
You cannot really know where does the proxy connect to. If there is more than one ip assigned to the name you're connecting to, one of them will be chosen at random.
viraptor
+1  A: 

You can use:

import socket
socket.gethostbyname('www.google.com')

this will return the IP address for the host. Don't pass 'http://www.google.com'. That will not work.

pma