File tree 1 file changed +11
-3
lines changed
1 file changed +11
-3
lines changed Original file line number Diff line number Diff line change 2
2
dataclass ,
3
3
)
4
4
import importlib
5
+ import ipaddress
5
6
import json
6
7
import logging
7
8
9
+ from furl import (
10
+ furl ,
11
+ )
8
12
from more_itertools import (
9
13
one ,
10
14
)
@@ -124,11 +128,15 @@ def public_ip() -> str:
124
128
"""
125
129
Return the public IPv4 address of the machine running this code.
126
130
"""
127
- url = 'https://checkip.amazonaws.com'
131
+ url = furl ( 'https://checkip.amazonaws.com' )
128
132
http = http_client (log )
129
- response = http .request ('GET' , url )
133
+ response = http .request ('GET' , str ( url ) )
130
134
assert response .status == 200 , R ('Unexpected response' , response )
131
- ip_address = response .data .decode ().strip ()
135
+ ip_address = response .data .decode ().strip () + '.0'
136
+ try :
137
+ ipaddress .IPv4Address (ip_address )
138
+ except ipaddress .AddressValueError as e :
139
+ raise ValueError ('Not a valid IPv4 address' , ip_address ) from e
132
140
return ip_address
133
141
134
142
You can’t perform that action at this time.
0 commit comments