Question

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

import urllib2

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

r = urllib2.urlopen(req)

Are there any functions like ip = urllib2.gethostbyname(req)?

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top