0

I have a python script python.py as follow:

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

and a php web page as follow:

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

        <button id="getlink">Get Link</button>
        <div id="output"></div>
<script>
        function handleSharedLink(evt) {
             exec(python getLink.py);
        }

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

Btw, I couldn't get the result from the python and show in the html div<'output'>. How can I show that? Any help will be appreciated!

EDITED: I edited my code as follow instead of running the python file in the javascript. But the result still doesn't show.

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

How can I improve that?

yimei
  • 383
  • 1
  • 3
  • 12
  • 1
    `exec` isn't a javascript builtin. What do you expect that to do? If you want to run server side code from the client you have to expose an api or rpc server, and send messages to that with ajax. – Håken Lid Nov 12 '17 at 15:45
  • You can't run Python from JavaScript running in a web browser. You could set up the Python code so that it's called by a web server, then have the JavaScript issue an ajax request to it. – Pointy Nov 12 '17 at 15:45
  • @Pointy I edited the code. Is that what you mean by issuing an ajax request to set up the Python code? – yimei Nov 12 '17 at 17:47
  • @yimei You cannot run python code directly from a JS file on client side as stated above. Run that python script on a remote servlet like a Flask and expose an API endpoint from which you'll get responses via AJAX (and send request via the same method) or just place that file on the server and run it as a subprocess called by an JS endpoint (again, ***backend..***), whichever way you find most suitable. – Milan Velebit Nov 12 '17 at 17:49

1 Answers1

1

I never came across any scenario to run ad-hoc scripts from javascript. Most of the cases, the server side scripts are exposed as APIs and can be triggered by simple AJAX calls.

I found a probable solution with little research which you might have already looked at it.

Call python function from JS

Note: this is best suites as comment. But I don't have enough reputation today to post a comment :)

ac194
  • 11
  • 1