如何使用javascript客户端设置Python服务器端 [英] How to set up Python server side with javascript client side

查看:98
本文介绍了如何使用javascript客户端设置Python服务器端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,已经有一个Python程序设置在我必须构建的控制台上运行。我将使用Javascript为应用程序构建Web GUI界面。
我如何:

So there is already a Python program set up that runs on the console that I must build upon. I will be building a web GUI interface for the application using Javascript. How would I:

a。在不触及原始代码的情况下,处理这个Python程序的输入/输出。

a. Go about handling the input/output of this Python program without touching the original code.

b。通过Javascript调用将控制台行输入发送到Python程序。我已经研究过原始HTTP请求/ AJAX,但我不确定如何将其作为输入发送到Python程序。

b. Go about sending console-line inputs to the Python program via Javascript calls. I've looked into raw HTTP requests/AJAX, but I'm not sure how exactly I would go about sending that as input to the Python program.

推荐答案

一个。处理程序的输入/输出: Pexpect 。它使用起来相当简单,阅读随附的一些示例应该足以让你了解基础知识。

a. To handle input/output of the program: Pexpect. It's fairly easy to use, and reading some of the examples distributed with it should teach you enough to get the basics down.

b。 Javascript界面​​:

b. Javascript interface:

好吧,我使用gevent和它的内置WSGI服务器。 (查看 WSGI服务器另一个)是)。我应该注意,该程序将保持状态,因此您可以通过将会话ID返回到javascript客户端并将pexpect会话存储在全局变量或其他容器中来管理打开的会话,以便您可以完成程序的输入和输出跨多个独立的AJAX请求。然而,我把它留给你,因为那不是那么简单。

Well, I use gevent and it's built-in WSGI server. (look up what a WSGI server (another) 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.

我所有的例子都是在点击你选择的东西后将POST请求放入一些。 (它实际上不会起作用,因为某些变量没有设置。设置它们。)

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.)

下面是相关部分:

<!-- JavaScript -->
<script src="jquery.js"></script>
<script type="text/javascript">
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) {
            <!-- do something here -->
            $('#somediv').html(data);
        }});
$("#someButton").bind('click', toPython(something));
</script>

然后服务器:

# 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()

如果你的程序是面向对象的,那么集成它是相当容易的。编辑:不需要面向对象。我现在已经包含了一些Pexpect代码

If your program is Object Oriented, it'd be fairly easy to integrate this. Doesn't need to be object oriented. and I have now included some Pexpect 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"

我建议的另一个选项是Nginx + uWSGI。如果你愿意,我也可以给你一些例子。它为您提供了将网络服务器整合到设置中的好处。

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.

这篇关于如何使用javascript客户端设置Python服务器端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆