Python Network Programming(1)---设备名与IPv4地址

本文介绍如何使用Python的socket模块获取本地计算机的主机名和IP地址,并演示了如何通过远程主机名获取其对应的IP地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 版本:python 2.7.6
  • 获取设备名的函数为gethostname()
  • 获取IP地址的函数为gethostbyname(host)

Key point

这两个函数在socket模块中。socket模块提供了类方法和实例方法。这两个函数属于类方法。

编写独立的print_machine_info()函数

__author__ = 'liyuan35023'

# !/home/mimiasd/PycharmProjects/workspace python27

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()

我们要在常用的main代码块中调用print_machine_info()函数。运行时,Python会为某些内部变量赋值,例如name。如果在命令行中运行脚本,name变量的值是main,即可以直接运行此函数。如果在其他脚本中调用,就需要先在其他脚本中导入,再手动调用此函数。

获取远程设备的IP地址

如果想知道远程设备的IP地址,可以使用内置的库函数gethostbyname(),其参数是远程设备的主机名。

import socket
import logging

def 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()
"Python Network Programming Cookbook, 2nd Edition" ISBN: 1786463997 | 2017 | PDF | 442 pages | 15.41 MB Key Features Solve real-world tasks in the area of network programming, system/networking administration, network monitoring, and more Familiarize yourself with the fundamentals and functionalities of SDN Improve your skills to become the next-gen network engineer by learning the various facets of Python programming Book Description Python is an excellent language to use to write code and have fun by prototyping a pplications quickly. The presence of lots of third-party libraries, also known as batteries, makes it easier and faster to prototype an application or to implement a new algorithm. If you are interested in creating the building blocks for many practical web and networking applications that rely on networking protocols, then this book is a must-have. It employs Python for network programming to solve a variety of problems. This book highlights the major aspects of network programming in Python, starting from writing simple networking clients to developing complex Software-Defined Networking (SDN) systems and programming the Internet. It creates the building blocks for many practical web and networking applications that rely on various networking protocols. It presents the power and beauty of Python to solve numerous real-world tasks in the area of network programming, system and network administration, network monitoring, and web-application development. In this edition, you will also be introduced to network modelling to build your own cloud network. You will learn about the concepts and fundamentals of SDN and then extend your network with Mininet. Next, you'll find recipes on Authentication, Authorization, and Accounting and other alternative vendor-specific SDN approaches and frameworks. You will also learn to configure the Linux Foundation networking ecosystem and automate your networks with Python. By the end of this book, you will be able to analyze your network security vulnerabilities using advanced network packet capture and analysis techniques.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值