Does Python's urllib2 have a gethostbyname function?
-
26-09-2019 - |
문제
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)
?
해결책
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()
.
다른 팁
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.
제휴하지 않습니다 StackOverflow