->基础环境

Linux:ubuntu 16.04

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

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐