【问题标题】:Get python data in an html/javascript webpage在 html/javascript 网页中获取 python 数据
【发布时间】:2019-05-10 19:41:48
【问题描述】:

所以我试图为 ctf 解决问题,而对于问题,我需要将数据从 python 脚本发送到 javascript。谁能告诉我怎么做?

我的html代码是:

<!DOCTYPE html>
<html>
    <head>

    </head>

    <body>
        <h1 class="text">text</h1>
        <script>
            $.get("[website]/cgi-bin/challenge.py", 
                function(data) {
                $(".text").html(data);
            });
        </script>
    </body>
</html>

我将网站替换为 [网站]。 python代码是这样的:

#!/usr/bin/python
import json

print "Content-type: text/html\n\n"
json.dumps("It works!")

【问题讨论】:

  • 发布的问题似乎根本没有包含any attempt 来解决问题。 StackOverflow 期待您 try to solve your own problem first,因为您的尝试有助于我们更好地了解您想要什么。请编辑问题以显示您尝试过的内容,以说明您在minimal reproducible example 中遇到的特定问题。欲了解更多信息,请参阅How to Ask 并拨打tour
  • 在那里我添加了我尝试过的代码。很抱歉 - 我不知道。

标签: python jquery json


【解决方案1】:
#!/usr/bin/python
import json

text = json.dumps("It works!")

print "Content-Type: application/json\n"
print text

【讨论】:

    【解决方案2】:

    你可以试试:https://www.zerorpc.io/,zerorpc 是服务器端进程之间的通信,可以让你将数据从服务器传输到客户端。

    在您的情况下,如果您想将数据从 python 传输到 node.js,您可以这样做:

    在你的 python 文件中作为服务器执行:

    import zerorpc
    
    class HelloRPC(object):
    def hello(self, name):
        return "Hello, %s" % name
    
    s = zerorpc.Server(HelloRPC())
    s.bind("tcp://0.0.0.0:4242")
    s.run()
    

    并在您的 node.js 代码中作为客户端:

     var zerorpc = require("zerorpc");
    
     var client = new zerorpc.Client();
     client.connect("tcp://127.0.0.1:4242");
    
     client.invoke("hello", "RPC", function(error, res, more) {
       console.log(res);
     });
    

    这将在控制台上打印“hello RPC”。

    【讨论】:

    • 我需要将数据从 python 发送到客户端 javascript,所以我可以在网站上更改一些内容 - 我不希望它打印到控制台。
    猜你喜欢
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 2019-05-05
    • 2019-02-19
    • 1970-01-01
    • 2013-04-27
    • 2019-01-13
    • 1970-01-01
    相关资源
    最近更新 更多