Search code examples
pythonurllib2gethostbyname

Does Python's urllib2 have a gethostbyname function?


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)?


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