0

Here is my Flask code. While it is not properly implemented and not working, it at least gives you an idea of what I am trying to do.

@app.route('/runid', methods=['GET','POST'])
def execCmdLocal(cmd):
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE,
                                shell=True )
    stdout, stderr = proc.communicate()
    status = proc.poll()
    if status != 0:
         print("Failed to execute command %s" % cmd)
    return status, stdout, stderr
 def runid()
 time.sleep(1)
 cmdStr = 'cd /root/Dart/log; ls -rt DartRunner*.log | tail -1 '
 ret = execCmdLocal(cmdStr)
 logName = ret[1].strip()
 print('The DartRunner Log generated is: %s'%logName)
 with open('/root/Dart/log/' + logName, "r") as fd:
    for line in fd:
        if 'runId' in line:
            runId = line.split()[-1]
            print('Run Id: %s'%runId)
            break
 print runID
 return runId

My original Python script is:

import os
from pprint import pprint
import time
def execCmdLocal(cmd):
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE,
                                shell=True )

    stdout, stderr = proc.communicate()
    status = proc.poll()

    if status != 0:
         print("Failed to execute command %s" % cmd)
    return status, stdout, stderr
time.sleep(1)
cmdStr = 'cd /root/Dart/log; ls -rt DartRunner*.log | tail -1 '
ret = execCmdLocal(cmdStr)
#pprint(ret)
logName = ret[1].strip()
print('The DartRunner Log generated is: %s'%logName)
with open(logName, "r") as fd:
    for line in fd:
        if 'runId' in line:
        runId = line.split()[-1]
        print('Run Id: %s'%runId)
        break;

The output I have is:

The DartRunner Log generated is: DartRunner-2018-06-07-145652.log Run Id: 180607-133

This function is printed when the whole program is started. But I am trying to get it only when I call my restAPI route.

Thanks in advance.

Zev
  • 3,423
  • 1
  • 20
  • 41
Rakesh Kumar
  • 63
  • 1
  • 10

1 Answers1

0

The code changes should be like this. By default Flask request is GET, I hope you just need to execute a process. Also, try to return a JSON_RESPONSE for a flask request. Try to learn Flask basics for a better understanding.

    @app.route('/runid')
    def runid():
        cmdStr = 'cd /root/Dart/log; ls -rt DartRunner*.log | tail -1 '
        ret = execCmdLocal(cmdStr)
        logName = ret[1].strip()
        print('The DartRunner Log generated is: %s'%logName)
        with open('/root/Dart/log/' + logName, "r") as fd:
        for line in fd:
            if 'runId' in line:
                runId = line.split()[-1]
                print('Run Id: %s'%runId)
                break
        return jsonify(runId)

    def execCmdLocal(cmd):
        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE,
                                    shell=True )
        stdout, stderr = proc.communicate()
        status = proc.poll()
        if status != 0:
             print("Failed to execute command %s" % cmd)
        return status, stdout, stderr   
yogi
  • 266
  • 2
  • 11
  • I see using json_response in flask But I think it is jsonify in flask Correct me If I am wrong – Rakesh Kumar Jun 08 '18 at 16:20
  • Yes absolutely - it should be `jsonify()`. I usually write overridden method over the jsonify() to do some pre-job before converting the response to json. You can just use jsonify() – yogi Jun 11 '18 at 04:59