【发布时间】:2014-04-20 17:44:06
【问题描述】:
我已经在我的 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()
【问题讨论】:
标签: python ssh web.py paramiko