changeset 34:a3fbe077a14c

replace wget with python method
author jingchunzhu <jingchunzhu@gmail.com>
date Thu, 23 Jul 2015 01:02:24 -0700
parents 7ceb967147c3
children d8dc482ef970
files runXena.py xena_utils.py
diffstat 2 files changed, 33 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/runXena.py	Wed Jul 22 13:24:44 2015 -0700
+++ b/runXena.py	Thu Jul 23 01:02:24 2015 -0700
@@ -69,13 +69,14 @@
 handler.setFormatter(formatter)
 logger.addHandler(handler)
 
-hostname = subprocess.check_output("hostname -f", shell=True).rstrip()
+hostname = xena.hostname() #subprocess.check_output("hostname -f", shell=True).rstrip()
 
 #
 # Check if there has been a Xena running on this system, and  
 # what its port number is or was.
 #
 xenaPort = xena.port()
+
 if xenaPort == None:
     # In this case, no Xena has been running on this sytem.
     xenaIsRunning = False
@@ -91,6 +92,7 @@
     #
     if xenaIsRunning:
         fp.write("Xena VM currently running on http://%s:%s\n" % (hostname, xenaPort))
+        fp.write("\n")
         fp.write("You can add http://%s:%s to Xena Data Hub\n" % (hostname, xenaPort))
     else:
         fp.write("Xena VM is not currently running on %s\n" % (hostname))
@@ -105,9 +107,11 @@
     if not xenaIsRunning:
         xenaPort = xena.port(testIfAvailable=True, findNewPort=True)
         fp.write("Starting Xena VM on http://%s:%s\n" % (hostname, xenaPort))
-        fp.write("You can add http://%s:%s to Xena Data Hub\n" % (hostname, xenaPort))
+        fp.write("\n")
+        fp.write("Use Xena Administration -> Check Status tool to review status.\n")
     else:
         fp.write("Xena VM already running on http://%s:%s\n" % (hostname, xenaPort))
+        fp.write("\n")
         fp.write("You can add http://%s:%s to Xena Data Hub\n" % (hostname, xenaPort))
     fp.close()
 
--- 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