mirror of https://github.com/flaggo/pydu.git
update doc for network
This commit is contained in:
parent
c8acf849e1
commit
1d11f2aadc
|
@ -42,3 +42,23 @@ Network
|
||||||
>>> from pydu.network import get_free_port
|
>>> from pydu.network import get_free_port
|
||||||
>>> get_free_port()
|
>>> get_free_port()
|
||||||
57118
|
57118
|
||||||
|
|
||||||
|
|
||||||
|
.. 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'
|
||||||
|
|
|
@ -123,6 +123,10 @@ def get_free_port():
|
||||||
# https://stackoverflow.com/questions/5619685/conversion-from-ip-string-to-integer-and-backward-in-python
|
# https://stackoverflow.com/questions/5619685/conversion-from-ip-string-to-integer-and-backward-in-python
|
||||||
# https://stackoverflow.com/questions/11894717/python-convert-ipv6-to-an-integer
|
# https://stackoverflow.com/questions/11894717/python-convert-ipv6-to-an-integer
|
||||||
def ip2int(ip_str):
|
def ip2int(ip_str):
|
||||||
|
"""
|
||||||
|
Convert ip to integer. Support IPV4 and IPV6.
|
||||||
|
Raise `ValueError` if convert failed.
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
return struct.unpack("!I", socket.inet_aton(ip_str))[0]
|
return struct.unpack("!I", socket.inet_aton(ip_str))[0]
|
||||||
except socket.error:
|
except socket.error:
|
||||||
|
@ -138,6 +142,10 @@ def ip2int(ip_str):
|
||||||
|
|
||||||
# https://stackoverflow.com/questions/5619685/conversion-from-ip-string-to-integer-and-backward-in-python
|
# https://stackoverflow.com/questions/5619685/conversion-from-ip-string-to-integer-and-backward-in-python
|
||||||
def int2ip(ip_int):
|
def int2ip(ip_int):
|
||||||
|
"""
|
||||||
|
Convert integer to ip. Support IPV4 and IPV6.
|
||||||
|
Raise `ValueError` if convert failed.
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
return socket.inet_ntoa(struct.pack("!I", ip_int))
|
return socket.inet_ntoa(struct.pack("!I", ip_int))
|
||||||
except (socket.error, struct.error):
|
except (socket.error, struct.error):
|
||||||
|
|
Loading…
Reference in New Issue