简体   繁体   English

客户端上的HTML按钮可在服务器上运行python脚本,然后将结果发送到客户端上的网页

[英]HTML button on client to run python script on server then send results to webpage on client

I have seen some previous questions, that were similar but I couldn't find anything like this. 我之前看过一些类似的问题,但找不到类似的问题。 I have a webpage (on a server) and I would like the user to click a button which will execute a python script. 我有一个网页(在服务器上),我希望用户单击一个将执行python脚本的按钮。 I want this python script to run on the server and then send the results back to the webpage and display it. 我希望此python脚本在服务器上运行,然后将结果发送回网页并显示。

When the user clicks the button, the data that will be sent to the server would be an XML file. 当用户单击按钮时,将发送到服务器的数据将是XML文件。

I just don't know where to start with all of this. 我只是不知道从哪里开始。 What can I use to accomplish this? 我可以用什么来做到这一点?

Thanks for your time. 谢谢你的时间。

EDIT: I actually have the webpage all done and setup, and it produces the XML. 编辑:实际上,我已经完成了所有网页的设置,并生成了XML。 I just need to run the python script when a user clicks on a button on the webpage. 当用户单击网页上的按钮时,我只需要运行python脚本。 Not sure if that helps, but I'm posting it. 不确定是否有帮助,但我正在发布。 Thanks 谢谢

I WOULD LIKE A HIGH-LEVEL EXPLANATION FOR THIS PLEASE AND THANK YOU, since I don't know about what has been suggested to me already. 为此,我想像是一个高层次的解释,谢谢,因为我不知道已经向我提出了什么建议。

There is a lot of web libs for python. python有很多网络库。 You may try bottle (work without installing, one-file, just put the „bottle.py” file in your work folder. A simple example: 您可以尝试使用bottle(无需安装即可工作,只有一个文件,只需将“ bottle.py”文件放入您的工作文件夹中即可。一个简单的示例:

    from bottle import route, run, static_file, post, request
    @route('/js/<filename>')
    def js(filename):
        return static_file(filename, root='js')

    @route('/')
    def index():return static_file('tst.html', root='./')

    @post('/xml')
    def xml():
        for x in request.forms:
            print(x)
        return {'return': 'accepted'}

    run(host='0.0.0.0', port=8000)

And html: 和html:

<!DOCTYPE html>
<html lang="ro">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>TTL</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script type="text/javascript" src="js/jquery.js"></script>
</head>
<body>
<button onclick="test()">Test</button>
<script type="text/javascript">
    function test() {
        $.ajax({
            url: 'xml',
            type: 'POST',
            data: '<my><xml>string</xml></my>',
            dataType: 'json',
            success: function (ret) {
                alert(ret['return']);
            }
        });
    }
</script>
</body>
</html>

Sorry for JQuery, to lazy to write plain js xhr. 对不起JQuery,懒得写普通的js xhr。 Bottle is well documented, but cherrypy, pyramid, django, tornado also. 酒瓶有据可查,但也有樱桃,金字塔,Django,龙卷风。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在服务器上运行JavaScript并将DOM结果发送到客户端 - Run javascript on server and send DOM results to client 脚本在服务器还是客户端上运行? - Script run on server or client? 如何通过单击 HTML 网页上的按钮运行 python 脚本? - How to run a python script from clicking a button on a HTML webpage? 如何通过单击按钮从 html 网页运行 python 脚本 - how to run python script from html webpage with button click Python套接字脚本&lt;-&gt; HTML客户端 - Python Socket script <-> HTML client 节点 JS - 将 function 本身发送到客户端,function 在客户端(不在服务器中)运行 - node JS - Send function itself to the client, that the function run in client (not in server) google.script.run.myFunction()不会从客户端HTML脚本调用服务器myFunction() - google.script.run.myFunction() doesn't call server myFunction() from client HTML script 使用html按钮运行python脚本 - run python script with html button 将数据从服务器脚本发送到客户端脚本(而不是相反) - Send data from Server script to Client script (and not the other way around) Java Server,html客户端,无法向服务器发送消息 - Java Server, html client, unable to send messages to server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM