利用socket包中的gethostname函数获取机器名之后,再调用gethostbyname函数获取机器的IP地址。
import socket
def print_machine_info():
host_name = socket.gethostname()
ip_address = socket.gethostbyname(host_name)
print "Host name: %s" %host_name
print "IP address: %s" %ip_address
if __name__ == "__main__":
print_machine_info()
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/8395.html