문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top