0

I have a DJANGO app (kwtest) that has VIEWS(a+b) and associated URL(s). When I access the first kw_dash (kw_dash.html) URL and press a button it calls a python script (from import) and should return this to the "return_results" (script_return.html) URL. The functionality is working fine but the output is "none" in the browser for "return_results". The script is also continuous and I can only stop by killing the SSH session (at the moment). I have searched for a while for the results and am after guidance (I am new to this). Any help much appreciated.

The script is working fine in console.

kw_dash.html;

{% load staticfiles %}
<html>
   <body> 
    <div class=”button”>
    <a href={% url 'kwtest:remote_results' %}>Run_Results</a>
    </div>
    </body>
</html>

script_return.html;

<html>
    <head>
        <title>Remote Results</title>
    </head>

    <body>
    <div>
    <pre>{{ output }}</pre>
    </div>
    </body>
</html>

viewa;

from django.shortcuts import render

def kw_dash(request):
    return render(request, 'kwtest/kw_dash.html', {})

viewb;

#- * -coding: utf - 8 - * -
from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseRedirect
import remote_results



def remote_results(request):
   import remote_results
   output = remote_results.remote_results()

   return render(request, 'kwtest/script_results.html', {
      'output': output
})

remote_results.py;

import paramiko

def remote_results():

    hostname ='192.168.xx.xx'
    port = 22
    username='xxxxxx'
    pkey_file = '/home/test/.ssh/id_rsa'

    key = paramiko.RSAKey.from_private_key_file(pkey_file)
    s = paramiko.SSHClient()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.connect(hostname, port, pkey=key)
    stdin, stdout, stderr = s.exec_command('cd /root/xxxxx/xxxx/ ;                                                                             python xxxxx.py', get_pty=True)
    print stdout.read()
    s.close()

if __name__=="__main__":
    remote_results()

The actual result should be the continuous results of the xxxxx.py in the correct url, currently when I force the script to stop the url then returns "none".

The xxxxx.py works in console.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Tealady
  • 5
  • 3

1 Answers1

0

remote_results() never returns anything, so its value is None.

Did you mean to return stdout.read() rather than just printing it?

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I believed that "print" would do it, will try return now. Thanks Daniel. Will get back to you. – Tealady Jun 17 '19 at 09:16
  • This now returns once I have killed the connection, thank you Daniel really appreciated. Any idea how I would amend to return "xyz" if no data is in the buffer as I have to manually kill the process? Sorry to ask for more. – Tealady Jun 17 '19 at 09:21