From 3a052331669a621a80f30c304766f0bcb44f2f56 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 18 Feb 2013 21:56:16 -0500 Subject: [PATCH] get-my-ip.py: Python 2.x sockets can't be used in 'with' statements They don't have __exit__. Use an explicit try/except block to be compatible with both 2.x and 3.x. --- setup/get-my-ip.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/setup/get-my-ip.py b/setup/get-my-ip.py index 286e67f..6b93ca1 100755 --- a/setup/get-my-ip.py +++ b/setup/get-my-ip.py @@ -7,9 +7,12 @@ import socket as _socket def get_my_ip(host, port=80): - with _socket.socket(_socket.AF_INET, _socket.SOCK_DGRAM) as s: + s = _socket.socket(_socket.AF_INET, _socket.SOCK_DGRAM) + try: s.connect((host, port)) return s.getsockname()[0] + finally: + s.close() if __name__ == '__main__': -- 2.26.2