Flask Socket IO server not detecting changes - python

Changes made to the application.py file don't seem to be detected by the server after I save the file, even though debug mode is on. The only way I've been able to see changes is by exiting the server and restarting with flask run
Here is the code for application.py:
import os
import requests
from flask import Flask, session, render_template, request, url_for, flash, redirect, jsonify
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config["SECRET_KEY"] = 'secret!'
socketio = SocketIO(app)
#app.route("/")
def index():
print('hello world')
return 'hello!'
if __name__ == '__main__':
socketio.run(app, debug=True)
And here's the command line/terminal:
λ flask run
* Serving Flask-SocketIO app "application.py"
* Forcing debug mode on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 156-884-244
(3824) wsgi starting up on http://127.0.0.1:5000
(3824) accepted ('127.0.0.1', 50569)
127.0.0.1 - - [10/Sep/2018 20:07:40] "GET /socket.io/?EIO=3&transport=polling&t=1536624459432-5 HTTP/1.1" 200 381 0.000000
(3824) accepted ('127.0.0.1', 50571)
127.0.0.1 - - [10/Sep/2018 20:07:40] "GET /socket.io/?EIO=3&transport=polling&t=1536624460314-6&sid=79eb8e587f664e3383c946bb046717ca HTTP/1.1" 200 215 0.000000
(3824) accepted ('127.0.0.1', 50568)
127.0.0.1 - - [10/Sep/2018 20:07:44] "GET /socket.io/?EIO=3&transport=websocket&sid=79eb8e587f664e3383c946bb046717ca HTTP/1.1" 200 0 4.610168
hello world
127.0.0.1 - - [10/Sep/2018 20:07:44] "GET / HTTP/1.1" 200 152 0.000000
hello world
127.0.0.1 - - [10/Sep/2018 20:07:58] "GET / HTTP/1.1" 200 152 0.000000
hello world
127.0.0.1 - - [10/Sep/2018 20:08:06] "GET / HTTP/1.1" 200 152 0.000000
wsgi exiting
(3824) wsgi exited, is_accepting=True
Those hello world's in command show up every time I change the text in print('hello world') and refresh the browser. Regardless of what I change it to, I always get the original version of the code's print argument.
Couple things I'm noticing:
this issue doesn't occur when I'm just running Flask. When I'm just running Flask, I see in the command line/terminal that changes were detected.
if I return a template of an HTML file, changes to the HTML file are automatically updated.

Hmm. It looks like the reloader does not work with you run the application via flask run. It does work, however, when you run it by running your application file (i.e. python application.py).
I'll log a bug and investigate.

Related

Regarding the problem of the route function according to the API object declaration location of Flask_restx

Flask restx is a library that develops api functions by dividing them into files in the flask framework and supports swaggerui.
When serving api and web pages on one flask server, the api part can be divided into files and developed using restx.
However, there is a caution when registering namespace to write flask restx.
The difference between the two source codes below is whether the namespace registration part of api and the app.route function part of api, and whether the namespace registration part of api comes before the app.route or later.
from flask import Flask, render_template, url_for
from datetime import datetime
from flask_restx import Api
from test import test
app = Flask(__name__)
#app.route('/')
def index():
return 'index'
api = Api(
app,
doc="/doc/",
version="0.1",
title="test",
)
api.add_namespace(test, '/test')
if __name__ == '__main__':
app.run(debug=True)
from flask import Flask, render_template, url_for
from datetime import datetime
from flask_restx import Api
from test import test
app = Flask(__name__)
api = Api(
app,
doc="/doc/",
version="0.1",
title="test",
)
api.add_namespace(test, '/test')
#app.route('/')
def index():
return 'index'
if __name__ == '__main__':
app.run(debug=True)
First, when executed with the first source code, it is normally recognized when approaching the / and /test path.
127.0.0.1 - - [27/Sep/2021 15:40:40] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [27/Sep/2021 15:40:40] "GET /test HTTP/1.1" 404 -
However, the second source code recognizes only /test normally and / does not.
127.0.0.1 - - [27/Sep/2021 15:40:40] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [27/Sep/2021 15:40:40] "GET /test HTTP/1.1" 200 -
It can be seen that the console log and web browser are also displaying 404 errors when approaching / route.
However, in the second source code, not all of the subroutes of / are not possible.
from flask import Flask, render_template, url_for
from datetime import datetime
from flask_restx import Api
from test import test
app = Flask(__name__)
api = Api(
app,
doc="/doc/",
version="0.1",
title="test",
)
api.add_namespace(test, '/test')
#app.route('/')
def index():
return 'index'
#app.route('/main')
def main():
return 'main'
if __name__ == '__main__':
app.run(debug=True)
In this way, / root is not recognized, but /main recognizes.
127.0.0.1 - - [27/Sep/2021 15:40:35] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [27/Sep/2021 15:40:40] "GET /test HTTP/1.1" 200 -
127.0.0.1 - - [27/Sep/2021 15:40:45] "GET /main HTTP/1.1" 200 -
I don't know why they do this.
Try leaving the route string empty to see if that works
#app.route('')
def index():
return 'index'

Flask application showing 404

I am new to Python and Flask and I need to work on a code base. I have the following files in directory called migration
Name
app
env
__pycache__
requirements.txt
run.py
In the run.py, I have a the following code:
from app import app
app.run(debug=True)
and inside the app directory, I have one __init__.py, which as code :
from app.helpers import get_page_display_name, get_page_url_name
# from app import views
from flask import Flask
app = Flask(__name__)
app.config['JSON_SORT_KEYS'] = False
app.jinja_env.globals.update(get_page_display_name=get_page_display_name)
app.jinja_env.globals.update(get_page_url_name=get_page_url_name)
Now I have a views.py file inside the same app folder and it has the route configuration and corresponding code like :
#app.route('/')
def index():
if 'username' in session:
return render_template("index.html")
return redirect(url_for('login'))
I am trying to run the application. I have used the following commands:
env/Scripts/activate
This has activated the environment and then:
$env:FLASK_APP=.\run.py
flask run
This has shown a message like it is running on http://127.0.0.1:5000 and printed the following messages when I opened the URL in browser:
127.0.0.1 - - [19/Nov/2020 10:23:15] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [19/Nov/2020 10:23:15] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [19/Nov/2020 10:23:16] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [19/Nov/2020 10:23:17] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [19/Nov/2020 10:23:18] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [19/Nov/2020 10:23:18] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [19/Nov/2020 10:23:18] "GET / HTTP/1.1" 404 -
I have also tried with
$env:FLASK_APP=.\app\views.py
flask run
This also has printed the same message as it is running on the same port, but when opened, the same 404 messages are being shown.
How can I run this application ? I have checked the documentation, but the structure is little different for this application. Thanks in advance for the help.
You have to import your views file after definition of app. I suggest you to use blueprint.
EDIT
views.py
from flask import Blueprint
bp = Blueprint('test', __name__, url_prefix='/')
#bp.route('/')
def index():
if 'username' in session:
return render_template("index.html")
return redirect(url_for('login'))
__init__py
from app.helpers import get_page_display_name, get_page_url_name
from views import bp
from flask import Flask
app = Flask(__name__)
app.register_blueprint(bp)
app.config['JSON_SORT_KEYS'] = False
app.jinja_env.globals.update(get_page_display_name=get_page_display_name)
app.jinja_env.globals.update(get_page_url_name=get_page_url_name)

Why do I get 404 error with basic Flask code?

My code
from flask import Flask, jsonify
app = Flask(__name__)
#app.route('/api')
def my_microservice():
return jsonify({'Hello': 'World!'})
if __name__ == '__main__':
app.run()
Terminal output
python flask_basic.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [20/Nov/2018 14:14:39] "GET / HTTP/1.1" 404 -
I have checked with lsof -i:5000 command and got nothing.
Why?
You set the route to /api (#app.route('/api')), yet you sent the request to /: "GET / HTTP/1.1".
Either change the route to / or send the request to /api

using for loops with bottle

I am currently developing a python web-app based on bottle so what I am trying to do is print the outcome of a for loop I have tried the following
#!/usr/bin/python
from bottle import *
#route('/')
def index():
for i in range(10):
return i
but this did not work and i got this from the development server output
localhost - - [13/Jan/2017 18:11:38] "GET /request HTTP/1.1" 200 0
localhost - - [13/Jan/2017 18:11:40] "GET /favicon.ico HTTP/1.1" 200 0
so i tried this
#!/usr/bin/python
from bottle import *
#route('/')
def index():
sumOfValues=0
for i in range(10):
sumOfValues+=i
return sumOfValues
this also did not work
and my devlopment server gave me this
localhost - - [13/Jan/2017 18:15:44] "GET /request HTTP/1.1" 500 746
localhost - - [13/Jan/2017 18:15:46] "GET /favicon.ico HTTP/1.1" 500 750
so how can I do it I tried searching google but nothing came back
, thanks in advance
Function has to return string - so use return str(sumOfValues)
If you return in a function, it immediately ends.
It looks like you want to do a streaming response of-sorts. Bottle can do this, but you must yield items.
See also:
Streaming Connection Using Python Bottle, Multiprocessing, and gevent

gevent.WSGIServer request method mystery

I'm getting some really strange behaviour when running gevent's WSGIServer. It seems like every request that comes through is having its method interpreted incorrectly..
If I send the following requests:
requests.get('http://localhost:5000')
requests.head('http://localhost:5000')
requests.delete('http://localhost:5000')
requests.put('http://localhost:5000')
requests.post('http://localhost:5000')
This is what appears in the console:
127.0.0.1 - - [2012-01-22 14:55:36] "POST / HTTP/1.1" 405 183 "-" "python-requests/0.9.1"
127.0.0.1 - - [2012-01-22 14:55:41] "DELETE / HTTP/1.1" 405 185 "-" "python-requests/0.9.1"
127.0.0.1 - - [2012-01-22 14:55:46] "16 / HTTP/1.1" 405 181 "-" "python-requests/0.9.1"
127.0.0.1 - - [2012-01-22 14:55:50] "8 / HTTP/1.1" 405 180 "-" "python-requests/0.9.1"
127.0.0.1 - - [2012-01-22 14:56:13] "HEAD / HTTP/1.1" 200 0 "-" "python-requests/0.9.1"
For completeness, this is the script I'm running:
from gevent.wsgi import WSGIServer
from flask import Flask
app = Flask(__name__)
app.debug = True
#app.route("/")
def hello():
return 'hello'
port = 5000
http_server = WSGIServer(('', port), app)
http_server.serve_forever()
What could be going on?
Edit:
I'm using gevent version: 0.13.0
Libevent has a limited support for HTTP methods and which HTTP methods are supported depends on a libevent version. Why you have a number instead of a method is obviously a bug. Could it be that you're building and linking gevent against different versions?
Can you try switching to gevent.pywsgi? That'll fix the issue at the cost of some performance.
Also the 1.0 version of gevent has a number of great improvements. You can get it there: http://code.google.com/p/gevent/downloads/list

Categories