无需使用网络服务器即可在浏览器中调用程序

Dil*_*ill 2 html python browser

有没有办法从本地HTML页面调用程序(Python脚本)?我在该页面上有一个YUI颜色选择器,需要通过rs232将其值发送到微控制器.(除了选择器之外还有其他东西,所以我无法编写应用程序而不是HTML页面.)

稍后,这将迁移到服务器,但我现在需要一个快速简便的解决方案.

谢谢.

Bli*_*ixt 6

我现在看到Daff提到了简单的HTTP服务器,但我就如何解决你的问题(使用BaseHTTPServer)做了一个例子:

import BaseHTTPServer

HOST_NAME = 'localhost'
PORT_NUMBER = 1337

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(s):
        s.send_response(200)
        s.send_header('Content-Type', 'text/html')
        s.end_headers()

        # Get parameters in query.
        params = {}
        index = s.path.rfind('?')
        if index >= 0:
            parts = s.path[index + 1:].split('&')
            for p in parts:
                try:
                    a, b = p.split('=', 2)
                    params[a] = b
                except:
                    params[p] = ''

        # !!!
        # Check if there is a color parameter and send to controller...
        if 'color' in params:
            print 'Send something to controller...'
        # !!!

        s.wfile.write('<pre>%s</pre>' % params)

if __name__ == '__main__':
    server_class = BaseHTTPServer.HTTPServer
    httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)

    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass

    httpd.server_close()
Run Code Online (Sandbox Code Playgroud)

现在,从你的JavaScript,你打电话 http://localhost:1337/?color=ffaabb