javascript - how to call python command from html page? -
i want call python command html page .
code :
<form class="form" action="" method="post" name="new-service" enctype=multipart/form-data> <div class="control-group"> <div class="controls"> <input id="search" name="search" type="text" placeholder="search.. eg: moto g" class="form-control input-xlarge search-query center-display" required=""> <!-- here want call python command or function --> </div> </div> </form>
and python command :
$ python main.py -s=bow
here bow input box .
use jquery send request python script on server handled cgi/fast cgi script or through wsgi (mod_wsgi apache) or mod_python (for apache too).
your python script receive input using query strings.
client side example:
<html><head><title>test</title> <script type="text/javascript" src="jquery.js"></script> <script> pyurl = "/cgi-bin/main.py"; // example. can anything. doesn't need python @ function callpy (argdict) { $.post(pyurl, argdict, function (data) { // here comes whatever you'll python's output. // example: document.write(data); }); } </script> </head><body> <!-- use it: --> <a href="#" onclick='javascript:callpy({"s": "bow", "someother": "argument"});'>click here test!!!</a> </body></html>
on server side (cgi example):
#! /usr/bin/env python import cgi, cgitb cgitb.enable() print "content-type: text/html\n" = cgi.fieldstorage() if i.has_key("s"): if i["s"].value=="bow": print "yeeeeey!!!! got 'bow'!!!<br>" else: print "woops! got", i["s"].value, "instead of 'bow'<br>" else: print "argument 's' not present!<br>" print "all received arguments:" x in i.keys(): print x, "<br>"
Comments
Post a Comment