文章目录
一、代码
import socket
from urllib.request import urlopen
def public():
with urlopen(r'http://ip.42.pl/raw') as fd:
f = fd.read().decode()
print(f)
def private():
print('当前主机名称为 : ' + socket.gethostname())
print('当前主机的IP为: ' + socket.gethostbyname(socket.gethostname()))
addrs = socket.getaddrinfo(socket.gethostname(), None)
for item in addrs:
print(item)
def get_ip(ip):
data = socket.gethostbyname(ip)
print(data)
if __name__ == '__main__':
private()
print('-----------------------------------------')
public()
print('-----------------------------------------')
get_ip('baidu.com')
二、返回结果
