How do I get a website's IP address using Python 3.x?

前端 未结 2 2041
再見小時候
再見小時候 2021-02-14 09:23

I have a string representing a domain name. How can I get the corresponding IP address using Python 3.x? Something like this:

>>> get_ip(\'http://www.st         


        
2条回答
  •  有刺的猬
    2021-02-14 09:57

    Python 3.1.3 (r313:86834, Nov 27 2010, 18:30:53) [MSC v.1500 32 bit (Intel)] on win32
    >>> import socket
    >>> socket.gethostbyname('cool-rr.com')
    '174.120.139.162'
    

    Note that:

    • gethostbyname() doesn't work with IPv6.
    • gethostbyname() uses the C call gethostbanme(), which is deprecated.

    If these are problematic, use socket.getaddrinfo() instead.

提交回复
热议问题