From: W. Trevor King Date: Tue, 19 Feb 2013 02:56:16 +0000 (-0500) Subject: get-my-ip.py: Python 2.x sockets can't be used in 'with' statements X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=3a052331669a621a80f30c304766f0bcb44f2f56;p=swc-boot-camp.git 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. --- 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__':