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

前端 未结 2 1241
傲寒
傲寒 2021-02-14 09:04

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:58

    >>> import socket
    
    >>> def get_ips_for_host(host):
            try:
                ips = socket.gethostbyname_ex(host)
            except socket.gaierror:
                ips=[]
            return ips
    
    >>> ips = get_ips_for_host('www.google.com')
    >>> print(repr(ips))
    ('www.l.google.com', [], ['74.125.77.104', '74.125.77.147', '74.125.77.99'])
    

提交回复
热议问题