Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>a. To handle input/output of the program: <a href="http://www.noah.org/wiki/pexpect" rel="noreferrer">Pexpect</a>. It's fairly easy to use, and reading some of the examples distributed with it should teach you enough to get the basics down. </p> <p>b. Javascript interface:</p> <p>Well, I use gevent and it's built-in WSGI server. (look up what a <a href="http://wsgi.readthedocs.org/en/latest/index.html" rel="noreferrer">WSGI server</a> (<a href="http://webpython.codepoint.net/wsgi_tutorial" rel="noreferrer">another</a>) is). I should note that this program will keep a state, so you can manage your open sessions by returning a session ID to your javascript client and storing your pexpect session in a global variable or some other container so that you can complete the program's input and output across multiple independent AJAX requests. I leave that up to you, however, as that is not as simple.</p> <p>All my example will do is put the POST request in some after clicking something of your choice. (it won't actually work because some of the variables are not set. Set them.)</p> <p>Heres the relevant parts:</p> <pre><code>&lt;!-- JavaScript --&gt; &lt;script src="jquery.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; function toPython(usrdata){ $.ajax({ url: "http://yoursite.com:8080", type: "POST", data: { information : "You have a very nice website, sir." , userdata : usrdata }, dataType: "json", success: function(data) { &lt;!-- do something here --&gt; $('#somediv').html(data); }}); $("#someButton").bind('click', toPython(something)); &lt;/script&gt; </code></pre> <p>Then the server:</p> <pre><code># Python and Gevent from gevent.pywsgi import WSGIServer from gevent import monkey monkey.patch_all() # makes many blocking calls asynchronous def application(environ, start_response): if environ["REQUEST_METHOD"]!="POST": # your JS uses post, so if it isn't post, it isn't you start_response("403 Forbidden", [("Content-Type", "text/html; charset=utf-8")]) return "403 Forbidden" start_response("200 OK", [("Content-Type", "text/html; charset=utf-8")]) r = environ["wsgi.input"].read() # get the post data return r address = "youraddresshere", 8080 server = WSGIServer(address, application) server.backlog = 256 server.serve_forever() </code></pre> <p>If your program is Object Oriented, it'd be fairly easy to integrate this. EDIT: Doesn't need to be object oriented. and I have now included some Pexpect code</p> <pre><code>global d d = someClass() def application(environ, start_response): # get the instruction password = somethingfromwsgi # read the tutorials on WSGI to get the post stuff # figure out WHAT to do global d success = d.doSomething() # or success = funccall() prog = pexpect.spawn('python someprogram.py') prog.expect("Password: ") prog.sendline(password) i = prog.expect(["OK","not OK", "error"]) if i==0: start_response("200 OK", [("Content-Type", "text/html; charset=utf-8")]) return "Success" elif i==1: start_response("500 Internal Server Error", [("Content-Type", "text/html; charset=utf-8")]) return "Failure" elif i==2: start_response("500 Internal Server Error", [("Content-Type", "text/html; charset=utf-8")]) return "Error" </code></pre> <p>Another option I suggest is Nginx + uWSGI. If you would prefer that, I can give you some examples of that as well. It gives you the benefit of incorporating the webserver into the setup.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload