重新提交表单web.py时,请保持SSH进程

时间:2014-04-20 17:44:07

标签: python ssh web.py paramiko

我已经在我的web.py服务器中实现了SSH命令。当用户按下按钮时,SSH命令将发送到我的其他Raspberry Pi,然后播放电影。但是,每次表单重置时,电影都会停止播放。

即使表单已刷新,我怎样才能让Python继续进行子进程(SSH)?

代码:

import web
from web import form
import os
import paramiko

cmd = 'cd /media/movies'
remove_bars = 'sudo sh -c "TERM=linux setterm -foreground black -clear >/dev/tty0" '
ah = 'omxplayer -o hdmi "American Hustle.mp4" <fifo &'
nsm = 'omxplayer -o hdmi "Now You See Me.mp4" <fifo &'

def makeSSH():
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect('192.168.1.115', username='pi', password='raspberry')
    stdin, stdout, stderr = ssh.exec_command(cmd)
    stdin, stdout, stderr = ssh.exec_command(remove_bars)
    return ssh;


# define the pages
urls = ('/', 'index')
render = web.template.render('templates')

app = web.application(urls, globals())


my_form = form.Form(
        form.Button("btn", id="btnA", value="A", html="Now You See Me", class_="btnA")
)

class index:
    # GET is used when the page is first requested
    def GET(self):
        form = my_form()
        return render.index(form, "RPi Remote Control")

    # POST is called when a web form is submitted
    def POST(self):
        # get the data submitted from the web form
        userData = web.input()

        if userData.btn == "A":
            print "TEST 1"
            ssh = makeSSH()
            stdin, stdout, stderr = ssh.exec_command(nsm)
        else:
            print "WHAT IS GOING ON?"


        raise web.seeother('/')


if __name__ == '__main__':
    app.run()

1 个答案:

答案 0 :(得分:-1)

我在/ media / movies /中添加了电影目录,并在time.sleep(10)之前添加了raise web.seeother('/')