Skip to content

Commit 4d6eb48

Browse files
iAmir97iAmir97
authored andcommitted
refactor get_ip
Signed-off-by: iAmir97 <Amir.balwel@embeddedllm.com>
1 parent a360b36 commit 4d6eb48

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

vllm/utils/network_utils.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)