systematic use of context managers for dealing with files instead of open()/close...
[nepi.git] / src / nepi / resources / linux / scripts / linux-udp-connect.py
index a4b662e..f8f8502 100644 (file)
@@ -139,9 +139,8 @@ if __name__ == '__main__':
     (local_host, local_port) = sock.getsockname()
 
     # Save local port information to file
-    f = open(local_port_file, 'w')
-    f.write("%d\n" % local_port)
-    f.close()
+    with open(local_port_file, 'w') as f:
+        f.write("%d\n" % local_port)
 
     # Wait until remote port information is available
     while not os.path.exists(remote_port_file):
@@ -155,9 +154,8 @@ if __name__ == '__main__':
     #       the read operation returns empty string!
     #       Maybe a race condition?
     for i in xrange(10):
-        f = open(remote_port_file, 'r')
-        remote_port = f.read()
-        f.close()
+        with open(remote_port_file, 'r') as f:
+            remote_port = f.read()
 
         if remote_port:
             break
@@ -174,9 +172,8 @@ if __name__ == '__main__':
     # TODO: Test connectivity!    
 
     # Create a ret_file to indicate success
-    f = open(ret_file, 'w')
-    f.write("0")
-    f.close()
+    with open(ret_file, 'w') as f:
+        f.write("0")
 
     # Establish tunnel
     tunchannel.tun_fwd(tun, remote,