用于显示主机名和 IP 地址的 Python 程序?
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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP