3

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.

2
  • letting random people from the internet to be able to trigger something on your server doesn't sound like a good idea. Commented May 3, 2020 at 15:13
  • Ofcourse the URL is going to have authentication in order to run from a browser. I already have few for PHP scripts have never done such for Python script. Commented May 3, 2020 at 15:15

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is super easy to setup just tested it locally and worked like a charm. Thank You
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.