Skip to content

Commit 84842a9

Browse files
authored
Merge pull request #514 from johngmyers/net
Improve documentation of net system module
2 parents 54a5583 + 498afdf commit 84842a9

File tree

1 file changed

+57
-27
lines changed

1 file changed

+57
-27
lines changed

docs/reference/model/net.md

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,45 @@ weight: 100
88

99
## split_host_port
1010

11-
`split_host_port(ip_end_point: str) -> List[str]`
11+
`split_host_port(ip_end_point: str) -> [str]`
1212

13-
Split the `host` and `port` from the `ip_end_point`.
13+
Splits an `ip_end_point` of the form `host:port` or `[host]:port` into a `host` and `port`.
14+
Returns a list containing the `host` and `port`, respectively.
15+
16+
An IPv6 literal in the `ip_end_point` must be enclosed in square brackets and will be returned
17+
without the enclosing brackets.
1418

1519
```python
1620
import net
1721

18-
host_and_port = net.split_host_port("B-K0NZJGH6-0048.local:80")
19-
host_port = net.join_host_port("B-K0NZJGH6-0048.local", 80)
22+
assert net.split_host_port("B-K0NZJGH6-0048.local:80") == ["B-K0NZJGH6-0048.local", "80"]
23+
assert net.split_host_port("[::1]:80") == ["::1", "80"]
2024
```
2125

2226
## join_host_port
2327

24-
`join_host_port(host, port) -> str`
28+
`join_host_port(host: str, port: int | str) -> str`
2529

26-
Merge the `host` and `port`.
30+
Combines `host` and `port` into a network address of the form `host:port`.
31+
If `host` contains a colon, as found in an IPv6 address literal, then returns `[host]:port`.
2732

2833
```python
2934
import net
3035

31-
host_and_port = net.split_host_port("B-K0NZJGH6-0048.local:80")
32-
host_port = net.join_host_port("B-K0NZJGH6-0048.local", 80)
36+
assert net.join_host_port("B-K0NZJGH6-0048.local", 80) == "B-K0NZJGH6-0048.local:80"
37+
assert net.join_host_port("::1", "80") == "[::1]:80"
3338
```
3439

3540
## fqdn
3641

3742
`fqdn(name: str = '') -> str`
3843

39-
Return Fully Qualified Domain Name (FQDN).
44+
Returns the Fully Qualified Domain Name (FQDN) of `name`, determined by
45+
performing a reverse DNS lookup on the first address returned by a forward DNS
46+
lookup. If `name` is specified as or defaults to the empty string, uses the system
47+
hostname for the forward DNS lookup.
48+
49+
This function is not supported on the WASM target.
4050

4151
```python
4252
import net
@@ -46,21 +56,24 @@ fqdn = net.fqdn()
4656

4757
## parse_IP
4858

49-
`parse_IP(ip) -> str`
59+
`parse_IP(ip: str) -> str`
5060

51-
Parse `ip` to a real IP address
61+
Parses the IP address `ip` and returns it in canonical form. If `ip` does
62+
not have valid syntax, returns the empty string.
5263

5364
```python
5465
import net
5566

56-
ip = net.parse_IP("192.168.0.1")
67+
assert net.parse_IP("192.168.0.1") == "192.168.0.1"
68+
assert net.parse_IP("2001:0db8:0:0:0:0:0:0") == "2001:db8::"
69+
assert net.parse_IP("invalid") == ""
5770
```
5871

5972
## to_IP4
6073

61-
`to_IP4(ip) -> str`
74+
`to_IP4(ip: str) -> str`
6275

63-
Get the IP4 form of `ip`.
76+
A synonym for `parse_IP()`.
6477

6578
```python
6679
import net
@@ -72,7 +85,7 @@ ip = net.to_IP4("192.168.0.1")
7285

7386
`to_IP16(ip) -> int`
7487

75-
Get the IP16 form of `ip`.
88+
A synonym for `parse_IP()`.
7689

7790
```python
7891
import net
@@ -82,9 +95,9 @@ ip = net.to_IP16("192.168.0.1")
8295

8396
## IP_string
8497

85-
`IP_string(ip: str | int) -> str`
98+
`IP_string(ip: str) -> str`
8699

87-
Get the IP string.
100+
A synonym for `parse_IP()`.
88101

89102
```python
90103
import net
@@ -96,48 +109,61 @@ ip = net.IP_string("192.168.0.1")
96109

97110
`is_IPv4(ip: str) -> bool`
98111

99-
Whether `ip` is a IPv4 one.
112+
If `ip` is a valid IPv4 address, returns `True`. Otherwise, returns `False`.
100113

101114
```python
102115
import net
103116

104-
ip = net.is_IPv4("192.168.0.1")
117+
assert net.is_IPv4("192.168.0.1") == True
118+
assert net.is_IPv4("::1") == False
119+
assert net.is_IPv4("invalid") == False
105120
```
106121

107122
## is_IP
108123

109124
`is_IP(ip: str) -> bool`
110125

111-
Whether `ip` is a valid ip address.
126+
If `ip` is a valid IPv4 or IPv6 address, returns `True`. Otherwise, returns `False`.
112127

113128
```python
114129
import net
115130

116-
ip = net.is_IP("192.168.0.1")
131+
assert net.is_IPv4("192.168.0.1") == True
132+
assert net.is_IPv4("::1") == True
133+
assert net.is_IPv4("invalid") == False
117134
```
118135

119136
## is_loopback_IP
120137

121138
`is_loopback_IP(ip: str) -> bool`
122139

123-
Whether `ip` is a loopback one.
140+
If `ip` is an IPv4 or IPv6 loopback address, returns `True`. Otherwise, returns `False`.
124141

125142
```python
126143
import net
127144

128-
isip = net.is_loopback_IP("127.0.0.1")
145+
assert net.is_loopback_IP("127.0.0.1") == True
146+
assert net.is_loopback_IP("127.1.2.3") == True
147+
assert net.is_loopback_IP("192.168.0.1") == False
148+
assert net.is_loopback_IP("::1") == True
149+
assert net.is_loopback_IP("::") == False
150+
assert net.is_loopback_IP("invalid") == False
129151
```
130152

131153
## is_multicast_IP
132154

133155
`is_multicast_IP(ip: str) -> bool`
134156

135-
Whether `ip` is a multicast one.
157+
If `ip` is an IPv4 or IPv6 multicast address, returns `True`. Otherwise, returns `False`.
136158

137159
```python
138160
import net
139161

140-
isip = net.is_multicast_IP("239.255.255.255")
162+
assert net.is_multicast_IP("239.255.255.255") == True
163+
assert net.is_multicast_IP("10.1.2.3") == False
164+
assert net.is_multicast_IP("ff02::1") == True
165+
assert net.is_multicast_IP("2001:0db8::") == False
166+
assert net.is_multicast_IP("invalid") == False
141167
```
142168

143169
## is_interface_local_multicast_IP
@@ -192,10 +218,14 @@ isip = net.is_global_unicast_IP("220.181.108.89")
192218

193219
`is_unspecified_IP(ip: str) -> bool`
194220

195-
Whether `ip` is a unspecified one.
221+
If `ip` is the IPv4 or IPv6 unspecified address, returns `True`. Otherwise, returns `False`.
196222

197223
```python
198224
import net
199225

200-
isip = net.is_unspecified_IP("0.0.0.0")
226+
assert net.is_unspecified_IP("0.0.0.0") == True
227+
assert net.is_unspecified_IP("10.0.0.1") == False
228+
assert net.is_unspecified_IP("::") == True
229+
assert net.is_unspecified_IP("2001:0db8::") == False
230+
assert net.is_unspecified_IP("invalid") == False
201231
```

0 commit comments

Comments
 (0)