3

I have a python script app.py in my local server (path=Users/soubhik.b/Desktop) that generates a report and mails it to certain receivers. Instead of scheduling this script on my localhost, i want to create an API which can be accessed by the receivers such that they would get the mail if they hit the API with say a certain id. With the below code i can create an API to display a certain text. But, what do i modify to run the script through this? Also if i want to place the script in a server instead of localhost, how do i configure the same?

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return ("hello world")


if __name__ == '__main__':
    app.run(debug=True)

Python Version is 2.7

1
  • Your code isn't really related to what you're actually asking. Commented Sep 3, 2018 at 6:03

3 Answers 3

3

A good way to do this would be to put the script into a function, then import that function in your Flask API file and run it using that. For hosting on a web server you can use Python Anywhere if you are a beginner else heroku is also a good option.

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

Comments

2

If you are tying to achieve something using Python-Flask API than you can have a close look at this documentations and proceed further https://www.flaskapi.org/, http://flask.pocoo.org/docs/1.0/api/

Apart from these here are few basic examples and references you can refer for a quickstart :

1-https://programminghistorian.org/en/lessons/creating-apis-with-python-and-flask

2- https://flask-restful.readthedocs.io/en/latest/

3- https://realpython.com/flask-connexion-rest-api/

Comments

1

You could do something like this

from flask import Flask
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

class ExecuteScript:
  def printScript:
    return "Hello World"

api.add_resource(ExecuteScript, '/printScript')

if __name__ == '__main__':
    app.run(debug=True)

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.