File tree Expand file tree Collapse file tree 1 file changed +8
-8
lines changed Expand file tree Collapse file tree 1 file changed +8
-8
lines changed Original file line number Diff line number Diff line change @@ -46,20 +46,20 @@ def get_ip() -> str:
4646 # IP is not set, try to get it from the network interface
4747
4848 # try ipv4
49- s = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
5049 try :
51- s .connect (("8.8.8.8" , 80 )) # Doesn't need to be reachable
52- return s .getsockname ()[0 ]
50+ with socket .socket (socket .AF_INET , socket .SOCK_DGRAM ) as s :
51+ s .connect (("8.8.8.8" , 80 )) # Doesn't need to be reachable
52+ return s .getsockname ()[0 ]
5353 except Exception :
5454 pass
5555
5656 # try ipv6
5757 try :
58- s = socket .socket (socket .AF_INET6 , socket .SOCK_DGRAM )
59- # Google's public DNS server, see
60- # https://developers.google.com/speed/public-dns/docs/using#addresses
61- s .connect (("2001:4860:4860::8888" , 80 )) # Doesn't need to be reachable
62- return s .getsockname ()[0 ]
58+ with socket .socket (socket .AF_INET6 , socket .SOCK_DGRAM ) as s :
59+ # Google's public DNS server, see
60+ # https://developers.google.com/speed/public-dns/docs/using#addresses
61+ s .connect (("2001:4860:4860::8888" , 80 )) # Doesn't need to be reachable
62+ return s .getsockname ()[0 ]
6363 except Exception :
6464 pass
6565
You can’t perform that action at this time.
0 commit comments