views:

451

answers:

8

Hi,

Is there a way to call a program (Python script) from a local HTML page? I have a YUI-colorpicker on that page and need to send its value to a microcontroller via rs232. (There is other stuff than the picker, so I can't code an application instead of an HTML page.)

Later, this will migrate to a server, but I need a fast and easy solution now.

Thanks.

+1  A: 

If you want an an HTML page to have some sort of server-side programming then you will need a webserver of some sort to do the processing.

My suggestion would be to get a web server running on your development box, or try to accomplish what you need to do with a local desktop application or script.

AaronS
A: 

No you need some kind of server. Wh not try out the portable webservers? You can run them from your usb drive.

Shoban
+3  A: 

Python has a small built in Web server. If you already already got Python to run with the RS232 you might need to read here on how to set up a very simple and basic webserver. An even easier one can look like this:

import SimpleHTTPServer
import SocketServer

port = 8000
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", port), Handler)
httpd.serve_forever()

Try so separate you source as good as possible, to that you won't have too much trouble to move it to a production ready Python capable webserver.

Daff
A: 

Try also XML-RPC it gives you a simple way to pass remote procedure calls from YUI towards a simple XMLRPC server and from that towards your rs232 device

DrFalk3n
+5  A: 

I see now that Daff mentioned the simple HTTP server, but I made an example on how you'd solve your problem (using 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()

Now, from your JavaScript, you'd call http://localhost:1337/?color=ffaabb

Blixt
+1  A: 

another quick solution is https://addons.mozilla.org/en-US/firefox/addon/3002 POW, it's a firefox extension that adds a simple web server with Server Side JS built in.

You'd be able to access a command line and call a python script from there.

Breton
A: 

blixt, its working. thanks :) but i have to use open localhost:1337

another question: would there be an easy way to poll sth from this easy webserver? this would be nice, but not needed. this is just some proof of concept, sadly webprogramming is something i dont know anything about. i like soldering and ASM better :)

Dill
Ah you're right, I changed the text. "Poll something from web server"? You mean you want the web server to send a request somewhere, or you want to request data from the web server? The web server returns data (see s.wfile.write(...) statement in code.) You can make it output whatever you want, then get it in JavaScript.
Blixt
Could you use the answers section for answers rather than comments and follow up questions please?
David Dorward
A: 

I see no reason why you can't setup a handler for .py/.bat/.vbs files in your browser. This should result in your chosen application running a script when you link to it. This won't work when you migrate to the server but as a testing platform it would work. Just remember to turn it off when you're done or you expose yourself to viruses from other sites.

SpliFF