Python 提供了两个级别访问的网络服务:
话不多说,还是直接上代码吧!
import socket
'''
通过域名获取IP
'''
def main(url):
if 'http://' in url:
name = url.replace('http://', '')
else:
name = url.replace('https://', '')
try:
info = socket.getaddrinfo(name, 80, 0, 0, socket.SOL_TCP)
print(info[0][4][0])
except socket.gaierror as err:
print(err)
if __name__ == '__main__':
f = open('./link.txt', 'r', encoding='utf-8')
data = f.read().split("\n")
for link in data:
print(link)
main(link)