python网络编程

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值