在javascrip中运行python

2024-04-20 05:02:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python脚本python.py,如下所示:

import requests
import json

url = "https://api.dropboxapi.com/2/sharing/list_shared_links"

headers = {
    "Authorization": "Bearer MY_ACCESS_KEY",
    "Content-Type": "application/json"
}

data = {
    "path": "/downloads/files.txt"
}

r = requests.post(url, headers=headers, data=json.dumps(data))

php网页如下:

^{pr2}$

顺便说一句,我无法从python得到结果并在html中显示div<'output'>。 我怎么能证明?任何帮助都将不胜感激!在

编辑: 我按如下方式编辑代码,而不是在javascript中运行python文件。但结果仍然没有显示出来。在

<html>
<script src='//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript'></script>

<input type="file" id="files" name="files[]" multiple />
<br/><br/>

<button id="getlink">Get Link</button>
<div id="output"></div>

<script>
 function handleSharedLink(evt) {

  $.ajax({     
     url: "https://api.dropboxapi.com/2/sharing/get_shared_links",

     headers:
        {
            "Authorization": "Bearer 1lzp0Ii5agAAAAAAAAAAJTVD-jRpVMcOfefVBzVkXOzM-W_LiFHSl7F9mgkd4bYk",
            "Content-Type": "application/json"
        },

    data:
        {
            "path": "TARCpace.apk"
        },
    success: function (data) {
            console.log(data);
    },
    error: function (data) {
            console.log(data);
    }
    r = requests.post(url, headers=headers, data=json.dumps(data))       
 })
}

  var output = document.getElementById('getlink').addEventListener('click', handleSharedLink, false);
  output.innerHTML = html(r);
</script>

</html> 

我该如何改进呢?在


Tags: importdivcomidjsonurloutputdata
1条回答
网友
1楼 · 发布于 2024-04-20 05:02:35

我从来没有遇到过从javascript运行特别脚本的场景。大多数情况下,服务器端脚本作为api公开,可以通过简单的AJAX调用触发。在

我找到了一个可能的解决办法,你可能已经看过了。在

Call python function from JS

注:这是最好的套房评论。但我今天没有足够的声誉发表评论:)

相关问题 更多 >