2

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.

3
  • 2
    Calling a pogrom from a web browser? Would that be a Flash mob? Commented Jun 22, 2009 at 7:05
  • Ah, someone corrected the original spelling... that rather defangs the pun... Commented Jun 22, 2009 at 7:29
  • 1
    I deleted the follow up post - simply, stackoverflow doesn't work as a regular forum, and such additions often get heavily down-voted very quickly. When you have enough reputation, it is fine to leave a "thanks" in a comment to specific replies - but not as an "answer". Commented Jun 22, 2009 at 8:20

7 Answers 7

6

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

Sign up to request clarification or add additional context in comments.

2 Comments

sir. what should be the content of my html? for example. localhost/test.html
Sir . how should i run this? what should be the content of my html?
3

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.

Comments

1

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.

Comments

1

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.

Comments

0

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

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.