当前位置 : 主页 > 编程语言 > python >

Python:通过socket.gethostbyname获取域名的ip地址

来源:互联网 收集:自由互联 发布时间:2022-10-26
方式一: # -*- coding: utf-8 -*-import socketip = socket.gethostbyname(www.baidu.com)print(ip)# 39.156.66.14 方式二: # -*- coding: utf-8 -*-import socketaddress_info = socket.getaddrinfo(www.baidu.com, None)ip = address_info[0][-1]

方式一:

# -*- coding: utf-8 -*- import socket ip = socket.gethostbyname("www.baidu.com") print(ip) # 39.156.66.14

方式二:

# -*- coding: utf-8 -*- import socket address_info = socket.getaddrinfo("www.baidu.com", None) ip = address_info[0][-1][0] print(ip) # 39.156.66.14
网友评论