网络通信模块

网络通信

1、获取一个算法在本地运行所需的数据
2、共享信息时间分布式处理
3、管理云服务

ipaddress模块

ipaddress 模块提供了一些类来验证、比较和处理IPv4/IPv6网络地址,用于查找网络上的地址和主机

Internet地址

地址

ip_addresses
import ipaddress
import binascii
ADDRESSSES=[
    "10.9.0.6",
    "fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa"
]
for ip in ADDRESSSES:
    addr=ipaddress.ip_address(ip)
    print('{!r}'.format(addr))
    print('IP version;',addr.version)
    print('is private:',addr.is_private)
    print('packed form:',binascii.hexlify(addr.packed))
    print('integer:',int(addr))
    print()
结果:
IPv4Address('10.9.0.6')
IP version; 4
is private: True
packed form: b'0a090006'
integer: 168361990

IPv6Address('fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa')
IP version; 6
is private: True
packed form: b'fdfd87b5b4755e3eb1bce121a8eb14aa'
integer: 337611086560236126439725644408160982186

网络

网络:用一个基本地址和一个掩码表示
掩码:指示地址的那些部分表示网络,那些部分表示该网络上的地址。

1、ipaddress_networks
NETWORK=[
    '10.9.0.0/24',
    'fdfd:87b5:b475:5e3e::/64',
]
for n in NETWORK:
    net=ipaddress.ip_network(n)
    print('{!r}'.format(net))
    print('is private:',net.is_private)
    print('broadcast:',net.broadcast_address)  #广播地址
    print('compressed:',net.compressed)
    print('with netmask:',net.with_netmask)
    print('with hostmask:',net.with_hostmask)
    print('num addresses:',net.num_addresses)
    print()

结果:
IPv4Network('10.9.0.0/24')
is private: True
broadcast: 10.9.0.255
compressed: 10.9.0.0/24
with netmask: 10.9.0.0/255.255.255.0
with hostmask: 10.9.0.0/0.0.0.255
num addresses: 256

IPv6Network('fdfd:87b5:b475:5e3e::/64')
is private: True
broadcast: fdfd:87b5:b475:5e3e:ffff:ffff:ffff:ffff
compressed: fdfd:87b5:b475:5e3e::/64
with netmask: fdfd:87b5:b475:5e3e::/ffff:ffff:ffff:ffff::
with hostmask: fdfd:87b5:b475:5e3e::/::ffff:ffff:ffff:ffff
num addresses: 18446744073709551616
2、ipaddress_network_iterate

网络实例是可以迭代的,会提供网络上的地址

NETWORK=[
    '10.9.0.0/24',
    'fdfd:87b5:b475:5e3e::/64',
]
for n in NETWORK:
    net=ipaddress.ip_network(n)
    print('{!r}'.format(net))
    for i,ip in zip(range(3),net):
        print(ip)
结果:
IPv4Network('10.9.0.0/24')
10.9.0.0
10.9.0.1
10.9.0.2
IPv6Network('fdfd:87b5:b475:5e3e::/64')
fdfd:87b5:b475:5e3e::
fdfd:87b5:b475:5e3e::1
fdfd:87b5:b475:5e3e::2
3、host方法

效果:需要查找能够由网络上常规主机使用的地址,可以使用host()方法,它会生成一个生成器

NETWORK=[
    '10.9.0.0/24',
    'fdfd:87b5:b475:5e3e::/64',
]
for n in NETWORK:
    net=ipaddress.ip_network(n)
    print('{!r}'.format(net))
    for i,ip in zip(range(3),net.hosts()):
        print(ip)
    print()
结果:
IPv4Network('10.9.0.0/24')
10.9.0.1
10.9.0.2
10.9.0.3

IPv6Network('fdfd:87b5:b475:5e3e::/64')
fdfd:87b5:b475:5e3e::1
fdfd:87b5:b475:5e3e::2
fdfd:87b5:b475:5e3e::3
网络支持in

除了迭代器协议,网络还支持in操作符,可以用来确定一个地址是否是一个网络的一部分

NETWORKS=[
    ipaddress.ip_network('10.9.0.0/24'),
    ipaddress.ip_network('fdfd:87b5:b475:5e3e::/64'),
]
ADDRESSS=[
    ipaddress.ip_address('10.9.0.6'),
    ipaddress.ip_address('10.7.0.31'),
    ipaddress.ip_address('fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa'),
    ipaddress.ip_address('fe80::3840:c439:b25e:63b0')
]
for ip in ADDRESSS:
    for net in NETWORKS:
        if ip in net:
            print('{}\nis on {}'.format(ip,net))
            break
    else:
        print('{}\nis not on a known network'.format(ip))
    print()
结果:
10.9.0.6
is on 10.9.0.0/24

10.7.0.31
is not on a known network

fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa
is on fdfd:87b5:b475:5e3e::/64

fe80::3840:c439:b25e:63b0
is not on a known network

接口

ipaddress_interfaces

网络接口:网络接口表示网络上的一个特定地址,可以表示为一个主机地址和一个网络前缀或网络掩码

ADDRESSES=[
    '10.9.0.6/24',
    'fdfd:87b5:5e3e:b1bc:e121:a8eb:14aa/64'
]
for ip in ADDRESSES:
    iface=ipaddress.ip_interface(ip)
    print('{!r}'.format(iface))
    print('network:\n',iface.network)
    print('ip:\n',iface.ip)
    print('IP with prefixlen:\n',iface.with_prefixlen)
    print('netmask:\n',iface.with_netmask)
    print('hostmask:\n',iface.with_hostmask)
    print()
结果:
IPv4Interface('10.9.0.6/24')
network:
 10.9.0.0/24
ip:
 10.9.0.6
IP with prefixlen:
 10.9.0.6/24
netmask:
 10.9.0.6/255.255.255.0
hostmask:
 10.9.0.6/0.0.0.255

IPv6Interface('fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa/64')
network:
 fdfd:87b5:b475:5e3e::/64
ip:
 fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa
IP with prefixlen:
 fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa/64
netmask:
 fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa/ffff:ffff:ffff:ffff::
hostmask:
 fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa/::ffff:ffff:ffff:ffff

你可能感兴趣的:(杂类,python,网络)