Skip to content

Commit 2a7ec99

Browse files
committed
11.04 小节完成~
1 parent 585e80d commit 2a7ec99

File tree

2 files changed

+135
-96
lines changed

2 files changed

+135
-96
lines changed

source/c11/p04_generate_range_of_ip_addresses_from_cidr_address.rst

Lines changed: 95 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -5,107 +5,111 @@
55
----------
66
问题
77
----------
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”))
1110

1211
|
1312
1413
----------
1514
解决方案
1615
----------
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+
>>>
8591
8692
|
8793
8894
----------
8995
讨论
9096
----------
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+
当你需要操作网络地址(比如解析、打印、验证等)的时候会很有用。
11199

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>`_

source/roadmap.rst

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,57 @@ Roadmap
2727

2828
::
2929

30-
| 前12章翻译完成
30+
| 前9章翻译完成
3131

32-
2015/04/01 - 2015/05/30:
32+
2015/04/01 - 2015/05/31:
3333

3434
::
3535

36-
| 15章翻译完成,包括附录
36+
| 10章翻译完成
3737

38+
2015/06/01 - 2015/06/30:
3839

39-
2015/06/01 - 2015/06/31:
40+
::
41+
42+
| 11章翻译完成
43+
44+
45+
2015/07/01 - 2015/07/31:
46+
47+
::
48+
49+
| 12章翻译完成
50+
51+
52+
2015/08/01 - 2015/08/31:
53+
54+
::
55+
56+
| 13章翻译完成
57+
58+
59+
2015/09/01 - 2015/09/30:
60+
61+
::
62+
63+
| 14章翻译完成
64+
65+
66+
2015/10/01 - 2015/10/31:
67+
68+
::
69+
70+
| 15章翻译完成
71+
72+
73+
2015/11/01 - 2015/11/15:
4074

4175
::
4276

4377
| 对全部翻译进行校对一次
4478

45-
2015/07/01 - 2015/07/10:
79+
80+
2015/11/16 - 2015/11/20:
4681

4782
::
4883

0 commit comments

Comments
 (0)