Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用web.py从表单提交启动的流程中动态输出值_Python_Web.py - Fatal编程技术网

Python 使用web.py从表单提交启动的流程中动态输出值

Python 使用web.py从表单提交启动的流程中动态输出值,python,web.py,Python,Web.py,我是web服务器新手,因此我需要一些关于如何实现以下目标的指导: 与服务器位于同一网络上的用户在服务器上打开一个页面(例如10.42.68.130:8080/周期) 用户将收到一个表单,要求他输入若干个周期 用户单击提交。提交激活另一个函数,该函数执行指定次数的for循环(我简化了所有这一切,所以它实际上应该在beaglebone black上触发GPIO引脚) 用户收到进度反馈,因此计数器显示已完成的周期数 任何帮助都将不胜感激 我要退出learnpythonthehardway.com第50

我是web服务器新手,因此我需要一些关于如何实现以下目标的指导:

  • 与服务器位于同一网络上的用户在服务器上打开一个页面(例如10.42.68.130:8080/周期)
  • 用户将收到一个表单,要求他输入若干个周期
  • 用户单击提交。提交激活另一个函数,该函数执行指定次数的for循环(我简化了所有这一切,所以它实际上应该在beaglebone black上触发GPIO引脚)
  • 用户收到进度反馈,因此计数器显示已完成的周期数
  • 任何帮助都将不胜感激

    我要退出learnpythonthehardway.com第50课和第51课的教程。这就是我设法做到的

    bin/app.py

    import web
    import time
    
    urls = ('/cycle', 'Index')
    
    app = web.application(urls, globals())
    
    render = web.template.render('templates/', base="layout")
    
    class Index(object):
        def GET(self):
            return render.hello_form2()
    
        def POST(self):
            form = web.input(cycles=0)
            cycleparams = "%s" % (form.cycles)
            cycleGPIO(form.cycles)
            return render.index2(cycleparams = cycleparams)
    
    if __name__ == "__main__":
        app.run()
    
    def cycleGPIO(cycles):
        for i in range(int(cycles)):
            print "cycle " + str(i+1)
            time.sleep(1)
    
    模板/layout.html

    $def with (content)
    
    <html>
    <head>
        <title>Test Page</title>
    </head>
    <body>
    
    $:content
    
    </body>
    </html>
    
    $def带(内容)
    测试页
    $:内容
    
    模板/index2.html

    $def with (cycleparams)
    
    $if cycleparams:
        Test completed with the following parameters:
        <br> 
        $cycleparams
    $else:
        No parameters specified
    <br><br>
    <a href="/cycle">Return to form</a>
    
    $def带(cycleparams)
    $if循环图:
    使用以下参数完成测试:
    
    $cycleparams $else: 没有指定参数

    模板/hello_form2.html

    <fieldset>
    <legend>Output Cycling</legend>
    
    <form action="/cycle" method="POST">
        <br>Number of cycles:
        <br><input type="number" name="cycles">
        <input type="submit">
    </form>
    </fieldset>
    
    
    输出循环
    
    循环次数:

    解决此问题的最佳方法是将web和GPIO循环过程分开。在此之后,您可以使用python中可用的许多进程间通信机制之一。关于这方面的一个好页面是

    现在,我将选择一种在两个python进程之间进行通信的最简单方法,即纯文本文件。我们有两个文件。一个用于web服务进程将表单输入发送到GPIO进程,另一个用于GPIO进程将反馈发送到web服务

    请记住,这只是一个示例,有许多更好的方法可以解决进程间通信问题。这只是给你一个粗略的想法,而不是你应该在任何生产系统中使用的东西

    这就是代码的样子

    web.py服务更改

    ...
    
    urls = ('/cycle-status', 'StatusIndex')
    
    ...
    
    class StatusIndex(object):
        def GET(self):
            # Read feedback params from a file
            cycleparams = read_gpio_feedback_file()
            return render.index2(cycleparams = cycleparams)
    
    class Index(object):
    
        ...
    
        def POST(self):
            form = web.input(cycles = 0)
            cycleparams = "%s" % (form.cycles)
            # Write cycle params to a file
            write_cycle_params(form.cycles)
            # This call probably won't produce any results 
            # because your GPIO process couldn't react so fast. 
            return render.index2(cycleparams = {})
    
    def write_cycle_params(cycles):
        f = open('/path/to/cycles/file.txt', 'w')
        for i in range(int(cycles)):
            f.write("cycle " + str(i + 1))
        f.close()
    
    def read_gpio_feedback():
        cycleparams = {}
        f = open('/path/to/feedback/file.txt', 'r')
        for line in f:
            cycleparams['param1'] = line
        f.close()
        return cycleparams
    
    GPIO过程

    #!/usr/bin/env python
    
    import sys
    
    if __name__ == '__main__':
        cycleparams = ""
        f = open('/path/to/cycles/file.txt', 'r')
        for line in f:
            cycleparams = cycleparams + line
        f.close()
    
        # Do something with cycleparams related to GPIO
        # Get your cycle params from GPIO pins
    
        ...
    
        cycleparams = ""
    
        f = open('/path/to/feedback/file.txt','w')
        f.write(cycleparams + '\n')
        f.close()
    
        sys.exit(0)
    

    您的GPIO进程必须定期运行,以从web服务生成的文件中读取信息,并写入web服务将解析和显示输出的反馈。您可以通过将GPIO进程添加到crontab中来实现这一点(或者任何其他进程调度机制都可以很好地工作)。

    感谢Boris的详尽回答!我肯定会很快浏览您提供的链接,并将拆分服务器和GPIO进程。我看到您在代码中所做的一件事是,每当服务器向客户机发出新的反馈时,都会重新提交页面。我似乎不知道如何在每次都不使用更新的信息呈现整个内容的情况下输出到页面。一种是长轮询,另一种是将数据从服务器推送到客户端(web浏览器)。在这两种情况下,您都必须维护客户端和服务器之间的长时间运行的连接,并且它们都需要客户端JavaScript代码。这个博客有一些很好的代码示例。