您的位置:首页 > 理论基础 > 计算机网络

python网络编程

2015-08-12 22:09 477 查看
python网络编程基础

python网络编程攻略学习笔记

打印设备和IPV4地址:

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)


socket.gethostname() 没有参数 获得所在主机或本地主机的名字

>>>help(socket.gethostname)
Help on built-in function gethostname in module _socket:

gethostname(...)
gethostname() -> string

Return the current host name.


socket.gethostbyname(hostname) 接受一个hostname 返回对应的ip

地址

>>>help(socket.gethostbyname)
Help on built-in function gethostbyname in module _socket:

gethostbyname(...)
gethostbyname(host) -> address

Return the IP address (a string of the form '255.255.255.255') for a host.


python手册查询信息:

socket.gethostname():

Return a string containing the hostname of the machine where the Python interpreter is currently executing.

If you want to know the current machine’s IP address, you may want to use gethostbyname(gethostname()). This operation assumes that there is a valid address-to-host mapping for the host, and the assumption does not always hold.

Note: gethostname() doesn’t always return the fully qualified domain name; use getfqdn() (see above).

socket.gethostbyname(hostname):

Translate a host name to IPv4 address format, extended interface. Return a triple (hostname, aliaslist, ipaddrlist) where hostname is the primary host name responding to the given ip_address, aliaslist is a (possibly empty) list of alternative host names for the same address, and ipaddrlist is a list of IPv4 addresses for the same interface on the same host (often but not always a single address). gethostbyname_ex() does not support IPv6 name resolution, and getaddrinfo() should be used instead for IPv4/v6 dual stack support.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: