Python Network Programming(1)---设备名与IPv4地址
来源:互联网 发布:小猫淘宝客 编辑:程序博客网 时间:2024/05/16 11:27
- 版本:python 2.7.6
- 获取设备名的函数为gethostname()
- 获取IP地址的函数为gethostbyname(host)
Key point
这两个函数在socket模块中。socket模块提供了类方法和实例方法。这两个函数属于类方法。
编写独立的print_machine_info()函数
__author__ = 'liyuan35023'# !/home/mimiasd/PycharmProjects/workspace python27import socketdef print_machine_info(): host_name = socket.gethostname() ip_address = socket.gethostbyname(host_name) print "Host name : %s"% host_name print "IP address : %s"% ip_addressif __name__ == '__main__': print_machine_info()
我们要在常用的main代码块中调用print_machine_info()函数。运行时,Python会为某些内部变量赋值,例如name。如果在命令行中运行脚本,name变量的值是main,即可以直接运行此函数。如果在其他脚本中调用,就需要先在其他脚本中导入,再手动调用此函数。
获取远程设备的IP地址
如果想知道远程设备的IP地址,可以使用内置的库函数gethostbyname(),其参数是远程设备的主机名。
import socketimport loggingdef get_remote_machine_info(): remote_host = 'www.pytgo.org' try: #print "IP address :%s" %socket.gethostbyname(remote_host) print_IP(remote_host) except BaseException, err_msg: print "%s: %s" %(remote_host,err_msg) logging.exception(err_msg)def print_IP(host_name): print "IP address :%s" %socket.gethostbyname(host_name)if __name__ == '__main__': get_remote_machine_info()
0 0
- Python Network Programming(1)---设备名与IPv4地址
- Python Network Programming(2)---地址转换函数
- IPV4与IPV6地址
- IPv4与IPv6地址
- python network programming tutorial
- python network programming tutorial
- Python Network Programming(7)---泊松分布与发包
- Python Network Programming(3)---通过指定的端口和协议找到服务名
- 使用Python实现获取设备名及地址
- Foundations of Python Network Programming
- Python 正则表达式验证IPv4地址
- [python&php 网络编程]格式化IPv4地址
- CCNA 1IPV4地址划分
- IPv4地址的存储与查询
- IPv4 地址
- IPV4地址
- IPv4 地址
- IPv4 地址
- 圣诞来了 h5游戏风靡朋友圈了
- Java 代码性能优化总结
- awk关联数组
- 算法学习:贪心求部分背包(使用结构体来求解)
- 解决X64操作系统PL/SQL连接报错问题
- Python Network Programming(1)---设备名与IPv4地址
- Linux中很多命令不支持|管道来传递参数解决方法 -- xargs
- Xcode统计代码行数
- tableView:实现导航栏渐变和顶部禁止弹簧效果
- python列表删除会出现一个错误 list index out of range
- UIImageView绕着某一个点旋转
- Android studio 导入github工程
- activiti参考5-历史任务
- 监听网络状态变化