My twilio_receive_msg.py file bears the following code:
from flask import Flask, request, redirect
import twilio.twiml
app = Flask(__name__)
#app.route("/", methods=['GET', 'POST'])
def hello_monkey():
"""Respond to incoming calls with a simple text message."""
resp = twilio.twiml.Response()
resp.message("Hello, Mobile Monkey")
return str(resp)
if __name__ == "__main__":
app.run(debug=True)
When I run python twilio_receive_msg.py, the code keeps running like forever and I see the following.
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: 135-602-230
Also, I am not able to open 'http://127.0.0.1:5000/'. I paste the link in my browser, a message appears that says waiting for 127.0.0.1.... and this message persists even after a long wait!
My OS is Windows and browser is chrome. I tried Explorer too but got the same message.
Can anybody assist me with this? Why am I not able to open the above link?
After long searches i tried localhost instead of 127.0.0.1 in the internet explorer and it worked.
Related
I copied this test code from the book and also downloaded it from the book's site. It runs and when I enter "http://localhost:5000/setuser/Fred", it replies "User value set to: Fred", but when I enter "http://localhost:5000/getuser", there is a 404 error.
Since I see no errors in the code, is there something in my Python environment that is causing this? Here is the short bit of code. Thanks from a beginner.
from flask import Flask, session
app = Flask(__name__)
app.secret_key = 'YouWillNeverGuess'
#app.route('/setuser/<user>')
def setuser(user: str) -> str:
session['user'] = user
return 'User value set to: ' + session['user']
#app.route('/getuser')
def getuser() -> str:
return 'User value is currently set to: ' + session['user']
if __name__ == '__main__':
app.run(debug=True)
* Serving Flask app 'quick_session' (lazy loading)
* Environment: development
* Debug mode: on
* Running on http://127.0.0.1:5000 (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 474-778-940
It turns out I needed to restart Windows. Then the code ran as expected.
Back in the old days we sang the tune "FDISK Format Reinstall Doo-Dah Doo-Dah"... Same song, newer beat.
#app.route('/')
def index():
return render_template("home.html")
My folder structure looks like this
tree-/
-static/
-styles.css
-templates/
-home.html
-app.py
I get
Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
On the browser and
127.0.0.1 - - [01/May/2021 09:41:47] "GET / HTTP/1.1" 404 -
In the debugger
Have looked at other related posts saying stuff about trailing slashes and it doesn't look like its making a difference, either I access
http://127.0.0.1:5000
or
http://127.0.0.1:5000/
I run my application using
app = Flask(__name__)
if __name__ == '__main__':
app.run()
I get
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
from flask import Flask, redirect, url_for, render_template
app = Flask(__name__)
if __name__ == '__main__':
app.run()
#app.route('/')
def index():
return "Hello World"
Problem is order of code.
Line app.run() has to be at the end of code because it runs endless code/loop (which gets requests from clients/browsers and sends reponses) and it blocks next lines of code. All code after app.run() is executed after server is closed.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return "Hello World"
if __name__ == '__main__':
app.run()
Move app.run() to the bottom.
Thanks to furas for this solution, hope this can help people looking at this thread
I'm running a Flask app in Cloud9. Whenever I start my Flask app, it says this message:
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
Is there a way to change this message? I'd like it to say something like this:
Connect to me at http://0.0.0.0:80/!
I've searched stack overflow and the web but couldn't find anything. I'm starting my app with app.run().
Also, is it possible to make the URL cyan?
You can change everything besides Running on http://0.0.0.0:80/ (Press CTRL+C to quit) by changing show_server_banner of flask.cli:
from flask import Flask
import sys
cli = sys.modules['flask.cli']
# put your own message here
cli.show_server_banner = lambda *x: click.echo("My nice message")
app = Flask(__name__)
app.run(host='0.0.0.0', port='80')
To get rid of the Running on http://0.0.0.0:80/ ... message, you can use unittest.mock:
from unittest import mock
from werkzeug._internal import _log
def my_startup_log(*args):
# log all messages except for the * Running on message
if not args[1].startswith(" * Running on"):
return _log(*args)
app = Flask(__name__)
with mock.patch('werkzeug.serving._log') as mocked:
# patch the logger object and replace with own logger
mocked.side_effect = my_startup_logger
app.run(host='0.0.0.0', port='8000')
This is very hacky and depends on the internal implementation of flask. Be careful when using this in production code, as this could easily break.
I am new to Flask ,as part of POC I am building a web app which takes some input and runs a long python program (it takes 3-4 hours) to run based on the user input.Now I read that I should use celery so that user need not wait for a response from my program , which would take a lot of time , instead I can redirect them to some page .
So here is my attempt at calling my long process via Celery
from flask import Flask,render_template
from forms import LoginForm
from test_webapp import testing_webapp
from flask import render_template, flash, redirect
from celery import Celery
app = Flask(__name__)
app.config['SECRET_KEY'] = 'This is secret key'
celery = Celery(app.name, broker= 'amqp://localhost//')
#celery.task
def call_demographic_data(brand,start_date,end_date,Country):
'''
This is a really long process
'''
testing_webapp(brand,start_date,end_date,Country)
#app.route('/index',methods=[ 'GET','POST'])
def index():
return "Query Submitted successfully"
#app.route('/',methods=[ 'GET'])
#app.route('/input',methods=[ 'GET','POST'])
def input():
form = LoginForm()
if form.validate_on_submit():
call_demographic_data.delay(form.brand.data,form.startdate.data,form.enddate.data,form.Country.data)
return redirect('/index')
return render_template('input.html', title='Enter a Brand', form=form)
#return ("should return index page")
if __name__ == '__main__':
app.run(debug=True)
but when I run my python script , nothing happens to interface, it is kind of stuck, it should have been redirected to index page (the one which shows that query has been successfully submitted)
Below is the console (Anaconda Prompt o/p)
(base) C:\Users\aadhikarla\Insight Auomation\app>python standard_routes.py
* Serving Flask app "standard_routes" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 908-258-463
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [26/Jun/2019 12:47:14] "GET /input HTTP/1.1" 200 -
It does not move forward from the GET request , I had changed the long program to just few print statements for testing celery , but it is not happening at all.
Also It is my understanding that by using Celery even if the user closes the browser , the Job will still persist and not stop , please let me know if my inference is correct as well
I am trying to get started with Flask, and I get 404 Not Found error for the hello world program.
The program I use:
from flask import Flask
app = Flask(__name__)
#app.route('/')
#app.route('/index.html')
def hello_world():
return 'Hello, World'
if __name__== '__main__':
app.run(host='localhost')
then I run
$export FLASK_APP = minimal.py
$flask run
Which returns:
* Serving Flask app "minimal"
* Forcing debug mode on
* Restarting with stat
* Debugger is active!
* Debugger PIN 127-804-808
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
When I visit http://localhost:5000 or http://127.0.0.1:5000, I get:
Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
And my host file looks like this:
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
Any idea whats wrong? Thank you
Yeah, you can remove the error 404 not found in following ways:
1: You should write command netstat -a -b to check which port is not free or busy doing something or
2: write in your code app.run(port=4996) instead of app.run() which by default choose port no. 50000.