Python 官方文档:入门教程 => 点击学习
->基础环境 linux:ubuntu 16.04 python ; 2.7 ->修改hostname 1:$sudo hostname 2tong-slavetwo 2:$sudo vi /etc/hostname 2
->基础环境
python ; 2.7
->修改hostname
1:$sudo hostname 2tong-slavetwo
2:$sudo vi /etc/hostname
2tong-slavetwo
3:$sudo vi /etc/hosts
127.0.0.1 localhost 2tong-slavetwo
127.0.1.1 2tong-slavetwo
->Python 环境下输出ip hostname
>>>import Socket
>>>hostname = socket.gethostname()
>>>print hostname
2tong-slavetwo
>>>ip = socket.gethostbyname(hostname)
>>>print ip
127.0.0.1
>>>ipList = socket.gethostbyname_ex(name)
>>>print ipList
('localhost', ['2tong-slavetwo', '2tong-slavetwo'], ['127.0.0.1', '127.0.1.1']
->札记
import socket后,会从/etc/hostname中寻找hostname,而后前往/etc/hosts中查找ip
--结束END--
本文标题: Python 获取主机ip与hostna
本文链接: https://lsjlt.com/news/191966.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0