Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Python scripts on a web server (e.g localhost)

Tags:

python

In my development of Android and Java applications I have been using PHP scripts to interact with an online MySQL database, but now I want to migrate to Python.

How does one run Python scripts on a web server? In my experience with PHP, I have been saving my files under /var/www folder in a Linux environment. Then I just call the file later with a URL of the path. Where do I save my Python scripts?

like image 240
mungaih pk Avatar asked Nov 27 '13 12:11

mungaih pk


People also ask

How do I run a python script from localhost?

Using the python Command To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!

Can you run a python script on a website?

It is possible to run Python in a web page (on the client side) using frameworks such as Pyjamas and Skulpt. If CGI / server sided python execution is not what you ale looking for, see : skulpt.org - this appears to execute python in the browser.


1 Answers

You can use Flask to run webapps.

The simple Flask app below will help you get started.

from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/sampleurl' methods = ['GET'])
def samplefunction():
    #access your DB get your results here
    data = {"data":"Processed Data"}
    return jsonify(data)

if __name__ == '__main__':
    port = 8000 #the custom port you want
    app.run(host='0.0.0.0', port=port)

Now when you hit http://your.systems.ip:8000/sampleurl you will get a json response for you mobile app to use.

From within the function you can either do DB reads or file reads, etc.

You can also add parameters like this:

@app.route('/sampleurl' methods = ['GET'])
def samplefunction():
    required_params = ['name', 'age']
    missing_params = [key for key in required_params if key not in request.args.keys()]

    if len(missing_params)==0:
        data = {
                "name": request.argv['name'],
                "age": request.argv['age']
               }

        return jsonify(data)
    else:
         resp = {
                 "status":"failure",
                 "error" : "missing parameters",
                 "message" : "Provide %s in request" %(missing_params)
                }
         return jsonify(resp)

To run this save the flask app in a file e.g. myapp.py

Then from terminal run python myapp.py

It will start the server on port 8000 (or as specified by you.)

Flask's inbuilt server is not recommended for production level use. After you are happy with the app, you might want to look into Nginx + Gunicorn + Flask system.

For detailed instruction on flask you can look at this answer. It is about setting up a webserver on Raspberry pi, but it should work on any linux distro.

Hope that helps.

like image 174
shshank Avatar answered Oct 25 '22 02:10

shshank