pydu/docs/network.rst

65 lines
1.4 KiB
ReStructuredText
Raw Normal View History

2017-11-29 00:25:02 +00:00
Network
-------
.. py:function:: pydu.network.dotted_netmask(mask)
Converts mask from /`xx` format to `xxx.xxx.xxx.xxx`.
``mask`` can be either ``int`` or ``str``.
>>> from pydu.network import dotted_netmask
>>> dotted_netmask('24')
'255.255.255.0'
>>> dotted_netmask(24)
'255.255.255.0'
2017-12-08 14:13:22 +00:00
.. py:function:: pydu.network.is_ipv4(ip)
2017-11-29 00:25:02 +00:00
2017-12-08 14:13:22 +00:00
Judge whether the given ``ip`` is IPV4 address.
2017-11-29 00:25:02 +00:00
2017-12-08 14:13:22 +00:00
>>> from pydu.network import is_ipv4
>>> is_ipv4('8.8.8.8')
2017-11-29 00:25:02 +00:00
True
2017-12-08 14:13:22 +00:00
>>> is_ipv4('localhost.localdomain')
2017-11-29 00:25:02 +00:00
False
2017-12-08 14:16:04 +00:00
2017-12-08 14:54:47 +00:00
.. py:function:: pydu.network.is_ipv6(ip)
Judge whether the given ``ip`` is IPV6 address.
>>> from pydu.network import is_ipv6
2017-12-29 10:40:14 +00:00
>>> is_ipv6('fe80::9e5b:b149:e187:1a18')
2017-12-08 14:54:47 +00:00
True
2017-12-29 10:40:14 +00:00
>>> is_ipv6('localhost.localdomain')
2017-12-08 14:54:47 +00:00
False
2017-12-08 14:16:04 +00:00
.. py:function:: pydu.network.get_free_port()
Get free port which could be bound.
>>> from pydu.network import get_free_port
>>> get_free_port()
57118
2018-01-23 12:14:02 +00:00
.. py:function:: pydu.network.ip2int(ip_str)
Convert ip to integer. Support IPV4 and IPV6.
Raise ``ValueError`` if convert failed.
>>> from pydu.network import ip2int
>>> ip2int('10.1.1.1')
167837953
.. py:function:: pydu.network.int2ip(ip_int)
Convert integer to ip. Support IPV4 and IPV6.
Raise ``ValueError`` if convert failed.
>>> from pydu.network import int2ip
>>> int2ip(167837953)
'10.1.1.1'