diff xena_utils.py @ 34:a3fbe077a14c

replace wget with python method
author jingchunzhu <jingchunzhu@gmail.com>
date Thu, 23 Jul 2015 01:02:24 -0700
parents 8bb037f88ed2
children d64a002c3b0c
line wrap: on
line diff
--- a/xena_utils.py	Wed Jul 22 13:24:44 2015 -0700
+++ b/xena_utils.py	Thu Jul 23 01:02:24 2015 -0700
@@ -6,29 +6,50 @@
 import os
 import socket
 import subprocess
+import httplib
 
 def jarPath():
     """Return the full pathname of the xena jar file"""
-    jarPath = os.getenv("XENA_JAR_PATH", "~")
+    jarPath = os.getenv('XENA_JAR_PATH', "~")
     return(os.path.join(jarPath, "xena.jar"))
 
 
 def baseDir():
-    return(os.getenv("XENA_BASE_DIR", "/tmp"))
+    return(os.getenv('XENA_BASE_DIR', "/tmp"))
 
 def fileDir():
     return(baseDir() + "/files")
 
+def hostname():
+    hostname = subprocess.check_output("hostname -f", shell=True).rstrip()
+    return hostname
+
 def isRunning(xenaPort):
     """Determine if Xena is running on the specified port"""
-    query = "wget -q -O- http://localhost:%s/data/'(+ 1 2)'" % xenaPort
+
+    host = hostname()
+    try:
+        httpServ = httplib.HTTPConnection(host, xenaPort)
+        httpServ.connect() 
+
+        data = "(+ 1 2)"
+        httpServ.request('POST', '/data/', data)
+        response = httpServ.getresponse()
+        if response.status == httplib.OK:
+            content = response.read()        
+    except:
+        return False
+
+    return (content == "3.0")
+    """
+    query = "wget -q -O- http://%s:%s/data/'(+ 1 2)'" % (hostname, xenaPort)
     try:
         result = subprocess.check_output(query, shell=True)
     except:
         return False
     else:
         return(result == "3.0")
-    
+    """
 
 def findUnusedPort():
     """Find a random port that is available on the local system, and return
@@ -51,18 +72,14 @@
         ss.close()
         return True
 
-
 def portFilename():
     """ Return the name of the file with the port of the running Xena, 
     if any
     """
-    xenaBaseDir = os.getenv("XENA_BASE_DIR", "~")
+    xenaBaseDir = os.getenv('XENA_BASE_DIR', "~")
     xenaPortFilename = xenaBaseDir + "/xena.port"
     return(xenaPortFilename)
 
-
-
-
 def port(testIfAvailable=False, findNewPort=False):
     preferredXenaPort = 7220
     xenaPort = None
@@ -79,6 +96,7 @@
                 #pid = subprocess.check_output(cmd, shell=True).rstrip()
                 #print "not available, used by",pid
                 xenaPort = None
+
     if findNewPort and xenaPort == None:
         if isPortAvailable(preferredXenaPort):
             xenaPort = preferredXenaPort