What HTTP Server should I use with python? - 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.

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

Flask hello world app not working in Spyder or Canopy

I have the following Flask app, and it is returning a 404 error. I have tried it in Spyder and Enthought Canopy, and have also used the
set FLASK_APP
and
flask run
commands at cmd, with Windows 10.
from flask import Flask
app = Flask(__name__)
#app.route("/admin")
def hello_world():
return "Hello World"
if __name__ == '__main__':
app.run(host="localhost", port=int("5000"))
I figured out whats wrong with your code.
You only have declared an admin route.
Accessing localhost:5000/admin gives you your desired output!
try changing#app.route('/admin') to #app.route('/')

Flask + Wsgi returning python shell script output

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.

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)

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