@@ -8,35 +8,45 @@ weight: 100
8
8
9
9
## split_host_port
10
10
11
- ` split_host_port(ip_end_point: str) -> List [str] `
11
+ ` split_host_port(ip_end_point: str) -> [str] `
12
12
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.
14
18
15
19
``` python
16
20
import net
17
21
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 " ]
20
24
```
21
25
22
26
## join_host_port
23
27
24
- ` join_host_port(host, port) -> str `
28
+ ` join_host_port(host: str , port: int | str ) -> str `
25
29
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 ` .
27
32
28
33
``` python
29
34
import net
30
35
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 "
33
38
```
34
39
35
40
## fqdn
36
41
37
42
` fqdn(name: str = '') -> str `
38
43
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.
40
50
41
51
``` python
42
52
import net
@@ -46,21 +56,24 @@ fqdn = net.fqdn()
46
56
47
57
## parse_IP
48
58
49
- ` parse_IP(ip) -> str `
59
+ ` parse_IP(ip: str ) -> str `
50
60
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.
52
63
53
64
``` python
54
65
import net
55
66
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" ) == " "
57
70
```
58
71
59
72
## to_IP4
60
73
61
- ` to_IP4(ip) -> str `
74
+ ` to_IP4(ip: str ) -> str `
62
75
63
- Get the IP4 form of ` ip ` .
76
+ A synonym for ` parse_IP() ` .
64
77
65
78
``` python
66
79
import net
@@ -72,7 +85,7 @@ ip = net.to_IP4("192.168.0.1")
72
85
73
86
` to_IP16(ip) -> int `
74
87
75
- Get the IP16 form of ` ip ` .
88
+ A synonym for ` parse_IP() ` .
76
89
77
90
``` python
78
91
import net
@@ -82,9 +95,9 @@ ip = net.to_IP16("192.168.0.1")
82
95
83
96
## IP_string
84
97
85
- ` IP_string(ip: str | int ) -> str `
98
+ ` IP_string(ip: str) -> str `
86
99
87
- Get the IP string .
100
+ A synonym for ` parse_IP() ` .
88
101
89
102
``` python
90
103
import net
@@ -96,48 +109,61 @@ ip = net.IP_string("192.168.0.1")
96
109
97
110
` is_IPv4(ip: str) -> bool `
98
111
99
- Whether ` ip ` is a IPv4 one .
112
+ If ` ip ` is a valid IPv4 address, returns ` True ` . Otherwise, returns ` False ` .
100
113
101
114
``` python
102
115
import net
103
116
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
105
120
```
106
121
107
122
## is_IP
108
123
109
124
` is_IP(ip: str) -> bool `
110
125
111
- Whether ` ip ` is a valid ip address.
126
+ If ` ip ` is a valid IPv4 or IPv6 address, returns ` True ` . Otherwise, returns ` False ` .
112
127
113
128
``` python
114
129
import net
115
130
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
117
134
```
118
135
119
136
## is_loopback_IP
120
137
121
138
` is_loopback_IP(ip: str) -> bool `
122
139
123
- Whether ` ip ` is a loopback one .
140
+ If ` ip ` is an IPv4 or IPv6 loopback address, returns ` True ` . Otherwise, returns ` False ` .
124
141
125
142
``` python
126
143
import net
127
144
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
129
151
```
130
152
131
153
## is_multicast_IP
132
154
133
155
` is_multicast_IP(ip: str) -> bool `
134
156
135
- Whether ` ip ` is a multicast one .
157
+ If ` ip ` is an IPv4 or IPv6 multicast address, returns ` True ` . Otherwise, returns ` False ` .
136
158
137
159
``` python
138
160
import net
139
161
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
141
167
```
142
168
143
169
## is_interface_local_multicast_IP
@@ -192,10 +218,14 @@ isip = net.is_global_unicast_IP("220.181.108.89")
192
218
193
219
` is_unspecified_IP(ip: str) -> bool `
194
220
195
- Whether ` ip ` is a unspecified one .
221
+ If ` ip ` is the IPv4 or IPv6 unspecified address, returns ` True ` . Otherwise, returns ` False ` .
196
222
197
223
``` python
198
224
import net
199
225
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
201
231
```
0 commit comments