python简单获取本机计算机名和IP地址的方法

yipeiwu_com6年前Python基础

本文实例讲述了python简单获取本机计算机名和IP地址的方法。分享给大家供大家参考。具体实现方法如下:

方法一:

>>> import socket
>>> hostname = socket.gethostname()
>>> print hostname
china-43226208c
>>>ip = socket.gethostbyname(hostname)
>>>print ip 
192.168.3.196 
>>> ipList = socket.gethostbyname_ex(hostname)
>>> ipList 
('china-43226208c', [], ['192.168.3.196'])

方法二:

>>> name = socket.getfqdn(socket.gethostname()) 
>>> name 
'china-43226208c'
>>> 

希望本文所述对大家的Python程序设计有所帮助。

相关文章

在python中画正态分布图像的实例

在python中画正态分布图像的实例

1.正态分布简介 正态分布(normal distribtution)又叫做高斯分布(Gaussian distribution),是一个非常重要也非常常见的连续概率分布。正态分布大家也...

Tensorflow 自带可视化Tensorboard使用方法(附项目代码)

Tensorflow 自带可视化Tensorboard使用方法(附项目代码)

Tensorboard: 如何更直观的观察数据在神经网络中的变化,或是已经构建的神经网络的结构。上一篇文章说到,可以使用matplotlib第三方可视化,来进行一定程度上的可视化。然而T...

Python 基础之字符串string详解及实例

Python字符串(string) 详解 及 代码 Python的字符串可以使用单引号('), 双引号("), 三引号('''); 三引号(''')里面, 可以添加单引号和双引号, 也可...

导入tensorflow:ImportError: libcublas.so.9.0 报错

导入tensorflow:ImportError: libcublas.so.9.0 报错

错误:ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory...

Python编程图形库之Pillow使用方法讲解

Python编程图形库之Pillow使用方法讲解

PIL vs Pillow PIL: Python Imaging Library,是python的图像处理库。由于PIL不兼容setuptools,再加上更新缓慢等因素,Alex Cl...