|
5 | 5 | ---------- |
6 | 6 | 问题 |
7 | 7 | ---------- |
8 | | -You have a CIDR network address such as “123.45.67.89/27,” and you want to generate |
9 | | -a range of all the IP addresses that it represents (e.g., “123.45.67.64,” “123.45.67.65,” …, |
10 | | -“123.45.67.95”). |
| 8 | +你有一个CIDR网络地址比如“123.45.67.89/27”,你想将其转换成它所代表的所有IP |
| 9 | +(比如,“123.45.67.64”, “123.45.67.65”, …, “123.45.67.95”)) |
11 | 10 |
|
12 | 11 | | |
13 | 12 |
|
14 | 13 | ---------- |
15 | 14 | 解决方案 |
16 | 15 | ---------- |
17 | | -The ipaddress module can be easily used to perform such calculations. For example: |
18 | | - |
19 | | ->>> import ipaddress |
20 | | ->>> net = ipaddress.ip_network('123.45.67.64/27') |
21 | | ->>> net |
22 | | -IPv4Network('123.45.67.64/27') |
23 | | ->>> for a in net: |
24 | | -... print(a) |
25 | | -... |
26 | | -123.45.67.64 |
27 | | -123.45.67.65 |
28 | | -123.45.67.66 |
29 | | -123.45.67.67 |
30 | | -123.45.67.68 |
31 | | -... |
32 | | -123.45.67.95 |
33 | | ->>> |
34 | | - |
35 | | ->>> net6 = ipaddress.ip_network('12:3456:78:90ab:cd:ef01:23:30/125') |
36 | | ->>> net6 |
37 | | -IPv6Network('12:3456:78:90ab:cd:ef01:23:30/125') |
38 | | ->>> for a in net6: |
39 | | -... print(a) |
40 | | -... |
41 | | -12:3456:78:90ab:cd:ef01:23:30 |
42 | | -12:3456:78:90ab:cd:ef01:23:31 |
43 | | -12:3456:78:90ab:cd:ef01:23:32 |
44 | | -12:3456:78:90ab:cd:ef01:23:33 |
45 | | -12:3456:78:90ab:cd:ef01:23:34 |
46 | | -12:3456:78:90ab:cd:ef01:23:35 |
47 | | -12:3456:78:90ab:cd:ef01:23:36 |
48 | | -12:3456:78:90ab:cd:ef01:23:37 |
49 | | ->>> |
50 | | - |
51 | | -Network objects also allow indexing like arrays. For example: |
52 | | - |
53 | | ->>> net.num_addresses |
54 | | -32 |
55 | | ->>> net[0] |
56 | | - |
57 | | -IPv4Address('123.45.67.64') |
58 | | ->>> net[1] |
59 | | -IPv4Address('123.45.67.65') |
60 | | ->>> net[-1] |
61 | | -IPv4Address('123.45.67.95') |
62 | | ->>> net[-2] |
63 | | -IPv4Address('123.45.67.94') |
64 | | ->>> |
65 | | - |
66 | | -In addition, you can perform operations such as a check for network membership: |
67 | | - |
68 | | ->>> a = ipaddress.ip_address('123.45.67.69') |
69 | | ->>> a in net |
70 | | -True |
71 | | ->>> b = ipaddress.ip_address('123.45.67.123') |
72 | | ->>> b in net |
73 | | -False |
74 | | ->>> |
75 | | - |
76 | | -An IP address and network address can be specified together as an IP interface. For |
77 | | -example: |
78 | | - |
79 | | ->>> inet = ipaddress.ip_interface('123.45.67.73/27') |
80 | | ->>> inet.network |
81 | | -IPv4Network('123.45.67.64/27') |
82 | | ->>> inet.ip |
83 | | -IPv4Address('123.45.67.73') |
84 | | ->>> |
| 16 | +可以使用 ``ipaddress`` 模块很容易的实现这样的计算。例如: |
| 17 | + |
| 18 | +.. code-block:: python |
| 19 | +
|
| 20 | + >>> import ipaddress |
| 21 | + >>> net = ipaddress.ip_network('123.45.67.64/27') |
| 22 | + >>> net |
| 23 | + IPv4Network('123.45.67.64/27') |
| 24 | + >>> for a in net: |
| 25 | + ... print(a) |
| 26 | + ... |
| 27 | + 123.45.67.64 |
| 28 | + 123.45.67.65 |
| 29 | + 123.45.67.66 |
| 30 | + 123.45.67.67 |
| 31 | + 123.45.67.68 |
| 32 | + ... |
| 33 | + 123.45.67.95 |
| 34 | + >>> |
| 35 | +
|
| 36 | + >>> net6 = ipaddress.ip_network('12:3456:78:90ab:cd:ef01:23:30/125') |
| 37 | + >>> net6 |
| 38 | + IPv6Network('12:3456:78:90ab:cd:ef01:23:30/125') |
| 39 | + >>> for a in net6: |
| 40 | + ... print(a) |
| 41 | + ... |
| 42 | + 12:3456:78:90ab:cd:ef01:23:30 |
| 43 | + 12:3456:78:90ab:cd:ef01:23:31 |
| 44 | + 12:3456:78:90ab:cd:ef01:23:32 |
| 45 | + 12:3456:78:90ab:cd:ef01:23:33 |
| 46 | + 12:3456:78:90ab:cd:ef01:23:34 |
| 47 | + 12:3456:78:90ab:cd:ef01:23:35 |
| 48 | + 12:3456:78:90ab:cd:ef01:23:36 |
| 49 | + 12:3456:78:90ab:cd:ef01:23:37 |
| 50 | + >>> |
| 51 | +
|
| 52 | +``Network`` 也允许像数组一样的索引取值,例如: |
| 53 | + |
| 54 | +.. code-block:: python |
| 55 | +
|
| 56 | + >>> net.num_addresses |
| 57 | + 32 |
| 58 | + >>> net[0] |
| 59 | +
|
| 60 | + IPv4Address('123.45.67.64') |
| 61 | + >>> net[1] |
| 62 | + IPv4Address('123.45.67.65') |
| 63 | + >>> net[-1] |
| 64 | + IPv4Address('123.45.67.95') |
| 65 | + >>> net[-2] |
| 66 | + IPv4Address('123.45.67.94') |
| 67 | + >>> |
| 68 | +
|
| 69 | +另外,你还可以执行网络成员检查之类的操作: |
| 70 | + |
| 71 | +.. code-block:: python |
| 72 | +
|
| 73 | + >>> a = ipaddress.ip_address('123.45.67.69') |
| 74 | + >>> a in net |
| 75 | + True |
| 76 | + >>> b = ipaddress.ip_address('123.45.67.123') |
| 77 | + >>> b in net |
| 78 | + False |
| 79 | + >>> |
| 80 | +
|
| 81 | +一个IP地址和网络地址能通过一个IP接口来指定,例如: |
| 82 | + |
| 83 | +.. code-block:: python |
| 84 | +
|
| 85 | + >>> inet = ipaddress.ip_interface('123.45.67.73/27') |
| 86 | + >>> inet.network |
| 87 | + IPv4Network('123.45.67.64/27') |
| 88 | + >>> inet.ip |
| 89 | + IPv4Address('123.45.67.73') |
| 90 | + >>> |
85 | 91 |
|
86 | 92 | | |
87 | 93 |
|
88 | 94 | ---------- |
89 | 95 | 讨论 |
90 | 96 | ---------- |
91 | | -The ipaddress module has classes for representing IP addresses, networks, and inter‐ |
92 | | -faces. This can be especially useful if you want to write code that needs to manipulate |
93 | | -network addresses in some way (e.g., parsing, printing, validating, etc.). |
94 | | -Be aware that there is only limited interaction between the ipaddress module and other |
95 | | -network-related modules, such as the socket library. In particular, it is usually not |
96 | | -possible to use an instance of IPv4Address as a substitute for address string. Instead, |
97 | | -you have to explicitly convert it using str() first. For example: |
98 | | - |
99 | | ->>> a = ipaddress.ip_address('127.0.0.1') |
100 | | ->>> from socket import socket, AF_INET, SOCK_STREAM |
101 | | ->>> s = socket(AF_INET, SOCK_STREAM) |
102 | | ->>> s.connect((a, 8080)) |
103 | | -Traceback (most recent call last): |
104 | | - File "<stdin>", line 1, in <module> |
105 | | -TypeError: Can't convert 'IPv4Address' object to str implicitly |
106 | | ->>> s.connect((str(a), 8080)) |
107 | | ->>> |
108 | | - |
109 | | -See “An Introduction to the ipaddress Module” for more information and advanced |
110 | | -usage. |
| 97 | +``ipaddress`` 模块有很多类可以表示IP地址、网络和接口。 |
| 98 | +当你需要操作网络地址(比如解析、打印、验证等)的时候会很有用。 |
111 | 99 |
|
| 100 | +要注意的是,``ipaddress`` 模块跟其他一些和网络相关的模块比如 ``socket`` 库交集很少。 |
| 101 | +所以,你不能使用 ``IPv4Address`` 的实例来代替一个地址字符串,你首先得显式的使用 ``str()`` 转换它。例如: |
| 102 | + |
| 103 | +.. code-block:: python |
| 104 | +
|
| 105 | + >>> a = ipaddress.ip_address('127.0.0.1') |
| 106 | + >>> from socket import socket, AF_INET, SOCK_STREAM |
| 107 | + >>> s = socket(AF_INET, SOCK_STREAM) |
| 108 | + >>> s.connect((a, 8080)) |
| 109 | + Traceback (most recent call last): |
| 110 | + File "<stdin>", line 1, in <module> |
| 111 | + TypeError: Can't convert 'IPv4Address' object to str implicitly |
| 112 | + >>> s.connect((str(a), 8080)) |
| 113 | + >>> |
| 114 | +
|
| 115 | +更多相关内容,请参考 `An Introduction to the ipaddress Module <https://docs.python.org/3/howto/ipaddress.html>`_ |
0 commit comments