Flask + Wsgi returning python shell script output - python

I have deployed two containers flask + wsgi and nginx I have a simple code which works returning hello world.
When I try to return the output of a python shell script to a webpage I get internal server error, the script it works via cli it even prints the output of docker ps.
Working code returns a simple hello world :
# app.py
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello world!'
if __name__ == '__main__':
app.run(host='0.0.0.0')
Not working code i get internal server error please help im not really sure why ... or how to debug it
#!/usr/bin/env python
import subprocess
def dockers():
call = subprocess.call('docker ps', shell=True)
return call
#!/user/bin/env python
from flask import Flask
from cont import dockers
app = Flask(__name__)
print(dockers())
#app.route('/')
def hello_world():
return dockers()
if __name__ == '__main__':
app.run(host='0.0.0.0')

Dont ever try to pass an object to a web page youll have a bad time. i wrote the result into a file split the lines to a list and returned it to the webpage.

Related

Getting Error 500 _____ after setting up basic Python Flask app with cPanel

I'm attempting to host a basic python app on a domain that I purchased through Namecheap and it's being hosted on cPanel. I've followed this tutorial word for word, however I'm receiving this 500 error (image below) and I can't seem to figure it out. Anyone have any suggestions on how to solve this issue? Thank you!
This is my app.py file:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
#app.route("/<string:name>/")
def say_hello(name):
return "Hello {name}!"
if __name__ == "__main__":
app.run()
This is my passenger_wsgi.py file:
from app import app as application

What HTTP Server should I use with python?

I'm trying to set up a HTTP sever on Google Cloud. I've run this with python and flask with no problems at all. When I go to the cloud console, create the app and deploy, with pure python and flask, an error occurs. Meanwhile, I tried using gunicorn but I get the same type of error.
My main.py file:
from flask import Flask, jsonify
app = Flask(__name__)
#app.route("/")
def hello():
return jsonify({"about": "Hello World"})
if __name__ == '__main__':
app.run(debug=True)
APP.YAML
env: flex
runtime: python
runtime_config:
pythom_version: 3
I expect to receive a string in JSON format like the following:
{
"about": "Hello World"
}
This is a typo: pythom_version, it should be: python_version.

Creating an API to execute a python script

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
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.
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/
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)

Built a new Flask app -- old one still showing in browser

I made the following file yesterday.
# import flask
from flask import Flask
from flask import render_template
from flask import request
app = Flask(__name__)
# create url & function mapping for root or /
#app.route('/')
def index():
return "Hello from Flask"
# create another mapping name /hello
#app.route('/hello')
def hello():
myName = "kayak"
return "Hello again !!" + myName
# create mapping for /myprofile
#app.route('/myprofile')
def showmyprofile():
return render_template('myprofile.html')
# create mapping for /myprofile
#app.route('/addprofileform')
def addprofileform():
return render_template('myprofileform.html')
# create a mapping for /addprofile
#app.route('/addprofile')
def addprofile():
myname = request.args.get('myname')
state_of_residence = request.args.get('state_of_residence')
return render_template('myprofile.html', html_page_name=myname,
html_page_state_of_residence=state_of_residence)
if __name__== '__main__':
app.run()
Then I made the following file today.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return 'This is the homepage'
if __name__ == "__main__":
app.run(debug=True)
I thought
app.run(debug=True)
would work to clear the old data, but I doesn't and http://127.0.0.1:5000/ page keeps showing "Hello from Flask".
How do I fix this?
Just clear the cache in your browser and try running it again.
Here's how to clear your cache in some browsers:
Firefix->https://support.mozilla.org/en-US/kb/how-clear-firefox-cache
Chrome->https://support.google.com/accounts/answer/32050?co=GENIE.Platform%3DDesktop&hl=en
You can export the FLASK_ENV environment variable and set it to development before running the server
export FLASK_ENV=development
flask run
This worked for me.
Running the program in incognito tab will not cause this error. No need to clear caches also. See https://support.google.com/chrome/answer/95464?co=GENIE.Platform%3DAndroid&hl=en

Python, Flask, 'Hello World': No browser reaction

I have pip-installed Flask and HTML5 on my Window-system. When I start the Hello World!-program with IDLE, I get a red message in the Python-Shell:
"* Running on xxxx://127.0.0.1:5000/". (xxxx = http)
And when I start it with app.run(debug=True) another red message appears:
"* Restarting with reloader".
My browser (Firefox) shows no reaction.
What can I do to get 'Hello World' in a new tab of Firefox?
The Code is:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(debug=True)
return and app.run are indended
You have to open a new tab with this url:
http://127.0.0.1:5000/
You need to actually open the page in your browser - it won't open itself. Open Firefox and navigate to
127.0.0.1:5000
(it's a URL)
When you run your code, it sits around waiting for a request from the user. When it gets a request, it'll return a response, and that's (sort of) what you see in your browser. Going to a URL is how you send that request - Flask will interpret anything sent to 127.0.0.1:5000 as a request, and try to match the URL to one of your #app.route decorators. For example, if you were to have a function decorated with #app.route("/hello"), then when you go to 127.0.0.1:5000/hello, Flask would run that function to determine the response.
Try out this code:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "<h1>Hello!</h1>"
if __name__ == "__main__":
from waitress import serve
serve(app, host="0.0.0.0", port=8080)
refrence Flask at first run: Do not use the development server in a production environment
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(debug=True)
Try this, this works for me. Open your firefox browser and go to the address given in the output. ex: http://XXXX.X.X.X:5000/

Categories

Resources