python interactive web data/forms/interface communicating with remote server
What's an efficient method (preferably simple as well) for communicating with a remote server and allowing the user to 'interact' with it (IE submit commands, user interface) via the web browser (IE a text box to input commands, and an text area for output, or various command-less abstracted interfaces)?
I have the 'standalone'开发者_开发问答 python code finished for communicating and working(terminal/console based right now). My primary concern is with re-factoring the code to suite the web, which involves establishing a connection (python sockets), and maintaining the connection while the user is logged on.
some further details:
- currently using django framework for the basic back end/templates.
Probably the most efficient would be to set up REST as fmsf said. In general, each command would correspond to an URL with other variables attached:
http://example.com/nuclear_warhead/activate/1
http://example.com/nuclear_warhead/activate/2
http://example.com/nuclear_warhead/activate/3
http://example.com/nuclear_warhead/position/1/AtlanticOcean
http://example.com/nuclear_warhead/position/2/NorthPole
http://example.com/nuclear_warhead/position/3/Moon
http://example.com/nuclear_warhead/launch/1
http://example.com/nuclear_warhead/launch/2
http://example.com/nuclear_warhead/launch/3
You could either have these as client actions (they click on a link or submit a form) or as Ajax calls. For Ajax calls, they fill out a complicated form, the form formats it into an acceptable URL with attached data, and sends it to the server. Once the server processes the commands, it returns a result (in XML or JSON format, usually) which is parsed by the browser and displayed on the page.
In a full RESTful app, you'd use the different HTTP methods of POST, GET, PUT, and DELETE to handle records
http://example.com/secret_document/1 [POST]
— creates the document
http://example.com/secret_document/1 [PUT]
— update the document
http://example.com/secret_document/1 [GET]
— retrieve the document
http://example.com/secret_document/1 [DELETE]
— delete the document
Not all browsers can support all HTTP methods, however.
In terms of implementation, Django is one option but a bit heavyweight for what you're looking for. You might want to look at this article which describes in full how you'd set up a lightweight application for responding to web client requests. You can definitely expand it out to add more functionality.
If you're using django and want the front-end (html in the browser) to talk with the server it should be easy for you to adapt to AJAX.
Here you go :) this will simplify your life
There is some crazy code in weberror.pdbcapture to do general interactivity through the web. It's implemented as WSGI middleware that basically listens for anyone to ask for input on sys.stdin, and when that happens it starts to get input from the web form and send output back to that same page. You may not want to use it directly, but it gives some ideas if you really want something like a console through the web (which it kind of sounds like you want, I'm not clear).
精彩评论