mercredi 29 juin 2016

Executing a Python file with Ajax

I am trying to run a python script on page load. The python script will save a log.text file with some log information. After doing some research I came up with:

    <script>
    window.onload = function () {
        $.ajax({
            type: 'POST',
            url: "img/test.py",
            success: function () {
                alert("working")
            },
            error: function () {
                alert("Not Working")
            }
        });
    };
</script>

Which works, everytime the page loads it alerts me that it was successful. And here is test.py:

#!/usr/local/bin/env python

try:
    txt = open(r'log.txt', "wb")
    txt.write("Success")
    txt.close()
except Exception,e:
    txt = open(r'log.txt', "wb")
    txt.write(e)
    txt.close()

But this is where the problem is. The file is not created and I don't know why.

More information: I am using godaddy as a hosting service. My script is written in python2.6. When I ssh in and use:

$ python path_to_file/test.py

It creates the file in the current cd location. So the server can run the file with python but when i use:

$ cd path_to_file
$ ./test.py

I get:

-bash: ./test.py: Permission denied

Is that a problem or is this normal? Everything else works I just seem to be missing one piece of this puzzle. Is my shebang statement correct? Why does my process not generate a log.txt file?

Aucun commentaire:

Enregistrer un commentaire