I have a Python script that prints and returns all image files from a folder in the server.
import os
images = ""
for dirname, dirnames, filenames in os.walk('..\directory'):
for filename in filenames:
images = images + os.path.join(dirname, filename) + "^"
print(images)
return images
I am calling this in Javascript using:
$.get("scripts/filename.py", function(data){
alert(data);
});
However, instead of the getting the 'printed' or returned data, it is just displaying the code from filename.py. Am I missing something here?
EDIT: By the way, I am using Google App Engine to host my website.
I have a Python script that prints and returns all image files from a folder in the server.
import os
images = ""
for dirname, dirnames, filenames in os.walk('..\directory'):
for filename in filenames:
images = images + os.path.join(dirname, filename) + "^"
print(images)
return images
I am calling this in Javascript using:
$.get("scripts/filename.py", function(data){
alert(data);
});
However, instead of the getting the 'printed' or returned data, it is just displaying the code from filename.py. Am I missing something here?
EDIT: By the way, I am using Google App Engine to host my website.
Share Improve this question edited Dec 1, 2015 at 21:53 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 25, 2012 at 5:11 John NgJohn Ng 8993 gold badges15 silver badges29 bronze badges 2- what framework are you using? just only the python cannot help you with what you are doing now, you should run the development server with some framework, e.g. django framework, easy and powerful one. – doniyor Commented Nov 25, 2012 at 5:42
- @doniyor I am using google app engine. – John Ng Commented Nov 25, 2012 at 5:44
3 Answers
Reset to default 2You need to have some Web Framework setup like Flask or cherrypy. I would suggest go with Flask, it's the easiest Web Framework to get started with.
And then you need to have some endpoint to which you can send an AJAX GET
Request, and then your Python script will return a JSON response. You can iterate through this JSON response to print out the results.
This code might work out for you:
import sys, os
from flask import Flask, request, url_for, render_template
@app.route('/images')
def index():
images = ""
for dirname, dirnames, filenames in os.walk('..\directory'):
for filename in filenames:
images = images + os.path.join(dirname, filename) + "^"
print(images)
# return a json encoded response
return Response(json.dumps(images, sort_keys=True, indent=4), mimetype='application/json')
if __name__ == '__main__':
# Bind to PORT if defined, otherwise default to 5000.
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
Now you need to start the server, and whenever you send a GET Request at localhost:5000/images
it will return the JSON response you're looking for.
Do you have Python set up to run on your server?
Configuring the Apache Web Server to Run Python on Windows or
How to Configure Apache to run Python on Windows
Just put the Phyton script in cgi folder