使用Python程序显示主机名和IP地址?
Python提供了gethostname(),gethostbyname()两个函数。gethostname()检索本地计算机的标准主机名。gethostbyname()从主机数据库中检索与主机名相对应的主机信息。
Socket. gethostname()Socket. gethostbyname()
算法
Step 1: use module socket. Step 2: use gethostname() retrives the standard host name for the local machine. Step 3: use gethostbyname() retrives host information corresponding to a host name from a host database.
范例程式码
# Display hostname andIP address
import socket
def host_IP():
try:
hname = socket.gethostname()
hip = socket.gethostbyname(hname)
print("Hostname: ",hname)
print("IP Address: ",hip)
except:
print("Unable to get Hostname and IP")
# Driver code
host_IP() #Function call输出结果
Hostname: Satyajit-PC IP Address: 192.168.1.66
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短