python网络编程

来源:互联网 发布:淘宝好评评语 编辑:程序博客网 时间:2024/06/15 23:58

python网络编程基础
python网络编程攻略学习笔记

打印设备和IPV4地址:

import 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_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.

0 0
原创粉丝点击