I wrote a simple Flask application on a VPS server in Apache
from flask import Flask, render_template, request
import sys
app = Flask(__name__)
#app.route("/")
def hello():
return "123"
if __name__ == "__main__":
app.run()
When I change the string return "123" to return "123456" and save it, it doesn't change when refreshing the site with Ctrl+F5.
It changes only when I restart the Apache server.
How can I change the file without a restart?
You can try reloading. Even that will impact the incoming traffic though.If there are more than one servers you can do a rolling reload or restart.
If not you can do a graceful restart of apache where traffic wont be impacted, using
apachectl -k graceful
You can do without a restart or reload as well.
The reason why you are getting older code is,
you might have a compiled python code(bytecode) file which will be served for every new request, if your script is myscript.py, check if there is a myscript.pyc, which is a bytecode containing the older version of the script. You need to remove that.
If you are using wsgi module for serving python in apache, you need to look at the following link on how to reload source code.
https://code.google.com/archive/p/modwsgi/wikis/ReloadingSourceCode.wiki
Related
I am trying to set up a server on a windows 10 machine, using Python and Flask, but it is not responding to external requests.
This is my server.py file:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "Hi there"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
When running, it says:
* Running on all addresses (0.0.0.0)
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://127.0.0.1:5000
* Running on http://195.XX.XXX.XXX:5000 (Press CTRL+C to quit)
Indeed, if I try to access it from that machine, using 127.0.0.1:5000 or 195.XX.XXX.XXX:5000, it works correctly.
However, when trying to access it from another machine, (using Chrome if that can be an issue), it just loads indefinitely, then says no data received, ERR_EMPTY_RESPONSE.
What is wrong with this? I've followed steps on the documentation so I don't get what could be wrong.
I also disabled firewall entirely on the windows 10 machine.
How you're running matters. If it's via
python server.py
then the app.run() will execute, and the server will bind to 0.0.0.0
But if you're running via some variant of
FLASK_APP=server.py flask run
then app.run() won't execute, and the server will bind to 127.0.0.1
In that case, adding --host=0.0.0.0 should fix things (unless you're having a firewall issue).
I have a Flask application that spins up my web application to a specific port (e.g., 8080). It launches by running python run.py. I also have a Python file that runs Selenium end-to-end tests for the web app.
This is part of a GitHub repo, and I have it hooked up so that whenever I push/commit to the GitHub repo, it initiates a Jenkins test. Jenkins is using my machine as the agent to run the commands. I have a series of commands in a Pipeline on Jenkins to execute.
Ideally, the order of commands should be:
Spin up the Flask web application
Run the Selenium tests
Terminate the Flask web application
Now, my issue is this: I can use Jenkins to clone the repo to the local workspace on my computer (the agent), and then run the Python command to spin up my web app. I can use Jenkins to run a task in parallel to do the Selenium tests on the port used by the web app.
BUT I cannot figure out how to end the Flask web app once the Selenium tests are done. If I don't end the run.py execution, it is stuck on an infinite loop, endlessly serving up the application to the port. I can have Jenkins automatically abort after some specified amount of time, but that flags the entire process as a failure.
Does anyone have a good idea of how to end the Flask hosting process once the Selenium tests are done? Or, if I am doing this in a block-headed way (I admit I am speaking out of copious amounts of ignorance and inexperience), is there a better way I should be handling this?
I'm not aware of an official way to cause the development server to exit under Selenium control, but the following route works with Flask 1.1.2
from flask import request
...
if settings.DEBUG:
#app.route('/quit')
def quit():
shutdown_hook = request.environ.get('werkzeug.server.shutdown')
if shutdown_hook is not None:
shutdown_hook()
return "Bye"
return "No shutdown hook"
According to the Flask documentation,
The flask script is nice to start a local development server, but you
would have to restart it manually after each change to your code. That
is not very nice and Flask can do better. If you enable debug support
the server will reload itself on code changes, and it will also
provide you with a helpful debugger if things go wrong.
To enable all development features (including debug mode) you can
export the FLASK_ENV environment variable and set it to development
before running the server:
$ export FLASK_ENV=development
$ flask run
However, in my very simple example, code changes still don't take effect until I restart the server. I've set up the particular script I want to run with export FLASK_APP=hello.py and the script is as follows:
from flask import Flask, url_for, request, render_template
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!!"
While the Flask development server is running, I change the return value by adding or removing an exclamation mark and saving the file. Then I refresh the page on http://127.0.0.1:5000/ in Chrome, but the number of exclamation points is unchanged. Once I quit Flask in the terminal using Ctrl-C and restart it and then refresh the page again, I get the proper number of exclamation points.
This is on a Mac, Python 3.6.0 (Anaconda), Flask 0.12.
Am I misunderstanding how the development server can help me, or is there anything else you think I should check? I'm quite new to Flask.
Try
FLASK_APP=app.py FLASK_DEBUG=1 TEMPLATES_AUTO_RELOAD=1 flask run
FLASK_DEBUG will get you the behavior you're looking for now; TEMPLATE_AUTO_RELOAD will get you the behavior you'll need as soon as you starting using templates.
$env:FLASK_ENV = "development"
flask run
That's worked for me on my Win10 laptop running VS code
Looking at app.config contents I discovered variable ENV='production'.
Set ENV='development' in config file and it worked. FLASK_ENV variable does not even exist.
i've wampserver with apache 2.4.4 installed in
I've installed python and i created a test file :
#!/Python34/python
print "Content-type: text/html"
print
print "<html><head>"
print ""
print "</head><body>"
print "Hello."
print "</body></html>"
i wanna know how to run this script ?
I personally don't like the way CGI and all this stuff work (slow to spawn processes, need to use some kind of tricks like "fastcgi" to bypass this, etc...)
I think you may build your Python programm as an HTTP server (Use cherrypy for example, or whatever you want.), start your Python program to listen on localhost:whatever, then, from the Apache side, just configure a proxy to localhost:whatever.
Pros:
No need for apache to fork a Python process each requests (An expensive operation)
You'll change your web server easily (like switch to Nginx) as nginx also support proxying.
You'll be able to start multiple Python servers and load balance between them
You'll be able to host your python servers on different host to load balance charge
You'll be able to completly bypass Apache if you put a Varnish in front of your app to cache results.
Cherrypy can automatically reload files if they are changed, no need to restart apache.
You'll stick to HTTP, no need to use protocols like fastcgi.
Easy to test on your dev machine without Apache, just point your browser to your localhost:whatever
Configuring apache 2 to pass your requests to your python daemon is as easy as:
<VirtualHost *:80>
ServerName example.com
ProxyPass / http://localhost:8080/
</VirtualHost>
And an hello world from the cherrypy documentation:
import cherrypy
class HelloWorld(object):
def index(self):
return "Hello World!"
index.exposed = True
cherrypy.quickstart(HelloWorld())
+1 to what Julien Palard says about not using CGI, it's really slow and inefficient. An alternative to running your server standalone and proxying through to it using Apache is to use mod_wsgi, which allows you to run Python processes inside the Apache process. Most web frameworks (Django, Bottle, Flask, CherryPy, web2py, etc) work well with it.
I'm trying to create a control system that will run on a Raspberry PI configured as a server. I'll have a python program running on the server that will control a process (i.e. temperature).
My question is how would I go about communicating the the running python program via a web browser? I'd like to be able to set a control point and get back the current process value.
I'm new to web development and have been looking at PHP and CGI, but that doesn't feel right to me. Someone also suggested communicating via SQL, where a script and the python program would both have access, but this doesn't feel right either?
What is the preferred method for doing this?
Thanks
The easiest thing would be install Flask:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def main():
return "Hey cool, it works!"
if __name__ == "__main__":
app.run("0.0.0.0", port=80, debug=True) # Might have to run as sudo for port 80