Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke/run a Python script using a web URL?

Tags:

python

url

web

I have a Python script that pulls data from a 3 rd party API. Currently this Pyhton script is automated on server side.

There are few instances where I have to toggle the script manually for new data updates. For the manual toggle I have to login to the server each time and run it from command line. Is there a way where I can create web url or something similar and just run that URL to make that script run from the browser address bar.

like image 435
error2007s Avatar asked May 08 '26 11:05

error2007s


2 Answers

One approach you could take is to use Flask, which is a minimal web framework. Here's an example of how you could use it:

from flask import Flask
from your_script import your_func

app = Flask(__name__)

@app.route('/run')
def run_command():
    your_func()
    return 'Executed your function!'

if __name__ == '__main__':
    app.run(debug=False, port=8080)

If you run this code you'd get a web server running on port 8080 that executes your function when you access the url. Here's a tutorial in the Flask documentation to get you started.

like image 152
linqo Avatar answered May 11 '26 00:05

linqo


I think the easiest way to do this is by using Flask.

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    # your code here
    return 'Hello, World!'
like image 31
Stévillis Avatar answered May 11 '26 00:05

Stévillis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!