I have tried the tutorial of python pyramid framework but, https connection, no matter how able to waitress.
http://docs.pylonsproject.org/projects/pyramid/en/latest/tutorials/wiki2/installation.html
If you look at the documents of waitress, there is an item called 'url_scheme' in pasteDeploy format. I tried to add the following to development.ini:
# # #
# Wsgi server configuration
# # #
[server: main]
use = egg:waitress#main
host = 0.0.0.0
port = 6543
url_scheme = https
But, it seems to be listening for http connections be performed pserve command.
$ serve development.ini - reload
Starting subprocess with file monitor
Starting server in PID 2757.
serving on http://0.0.0.0:6543
There is no response when accessed by browser in this state.
Application I'm trying to create is expecting a https access, but do you think there is a package needed for something else. Or Do I fundamentally wrong somewhere? I would appreciate the advice of experts.
Environment in fedora19, python 3.3.2. the following packages that are included in the virtualenv:
Chameleon == 2.12
Mako == 0.9.0
MarkupSafe == 0.18
PasteDeploy == 1.5.0
Pygments == 1.6
SQLAlchemy == 0.8.2
WebOb == 1.2.3
coverage == 3.7
nose == 1.3.0
pyramid == 1.4.5
pyramid-debugtoolbar == 1.0.8
pyramid-mako == 0.2
pyramid-tm == 0.7
repoze.lru == 0.6
transaction == 1.4.1
translationstring == 1.1
tutorial == 0.0
venusian == 1.0a8
waitress == 0.8.7
zope.deprecation == 4.0.2
zope.interface == 4.0.5
zope.sqlalchemy == 0.7.3
Please tell us the location of the document would be helpful to me means.
Thank you very much!
Waitress does not actually support decoding https requests. The only way to support https is by putting waitress behind a reverse proxy such as nginx. You then allow nginx to decrypt the request and pass it on to waitress. The problem here is that waitress now thinks it's serving an http request because thats what it sees from nginx. The url_scheme setting is for telling waitress that all requests coming into waitress are actually https, which it can then forward on to the application, which uses that fact to help your application generate urls using the https scheme instead of http.
Hopefully that makes sense but either way it should be clear to you that your https setup is not going to work when no where in your pastes have you actually created a certificate or a private key.
Related
I have been running flask-talisman on my development server and everything checks out fine. Yet, with the same code and requirements installed on my dedicated server for production (Almalinux), just adding Talisman(app) after app = Flask(__name__) results in the webpage not loading with a redirection to https://localhost:8000. The error message that I precisely get on my browser after typing in the domain is:
This site can't be reached - localhost refused to connect
I am running Nginx 1.14.1 with gunicorn 20.1.0 and supervisor. The server is connected to the internet and without using Talisman it has run smoothly so far.
List of things that I tried without any effect
temporarily stopped firewall
restarted nginx
both tried to access the website through its domain and IP address - the redirection to localhost:8000 remains
tried to run the app on other ports, e.g. 8000 for testing
stripped down the code to a mere mini tutorial that runs well on my development server but not on my production server. So I figured it can't be the app itself.
checked error logs and there is literally nothing, not in the nginx error log or python app error log. Access log shows nothing usual, the same as if everything checks out.
searched the Internet and found nothing that would point in the right direction and explain the failed redirect to localhost:8000
Here is a stripped down tutorial code that should run but doesn't run on my server:
from flask import Flask
from flask_talisman import Talisman
app = Flask(__name__)
Talisman(app)
app.secret_key = 'kungfoo'
#app.route('/', methods=['GET', 'POST'])
def index():
return "Hello stackoverflow!"
if __name__ == "__main__":
app.run(debug=True)
Well,
proxy_set_header X-Forwarded-Proto $scheme;
does the trick in the nginx.conf within the server's location / {} block. This is stated in the gunicorn docs and can be easily missed...
It is recommended to pass protocol information to Gunicorn. Many web
frameworks use this information to generate URLs. Without this
information, the application may mistakenly generate ‘http’ URLs in
‘https’ responses, leading to mixed content warnings or broken
applications.
I've deployed a flask-socketio web server, but after installing zerorpc which installs gevent i'm facing a lot of troubles..
at first my code looked like this:
socketio.start_background_task(poll_events)
socketio.run(app, host="0.0.0.0", keyfile='key.pem', certfile='cert.pem')
I'm starting a background task which will constantly read from a queue and send messages through socketio. now that gevent is installed it flask-socketio will try to use it (which i'm fine with actually making my server a production server and not a development one) but then socketio.start_background_task blocks. So I read that
from gevent import monkey; monkey.patch_all()
is required.
So now my code looks like that:
socketio.start_background_task(poll_events)
WSGIServer(('0.0.0.0', 5000), app, keyfile='key.pem', certfile='cert.pem').serve_forever()
For some reason when debugging with pycharm I received a lot of weird greenlet exceptions and also I think that sometimes socketio messages are dropped so I decided to use eventlet. Then again, patching is required. So my code looks like this:
socketio.start_background_task(poll_events)
eventlet.wsgi.server(eventlet.wrap_ssl(eventlet.listen(("0.0.0.0", 5000)), keyfile='key.pem', certfile='cert.pem'), app)
Because of monkey patching zerorpc throws an exception
"gevent.exceptions.LoopExit: This operation would block forever"
What is the correct way to deploy a production server with flask + socketio + zerorpc?
I've resolved the issue, when debugging I choose "threading" as async_mode
socketio = SocketIO(app, async_mode="threading")
and when deploying with gunicorn I use gevent
CMD ["gunicorn", "-w", "1", "-k", "gevent","--reload", "web_app:app"]
For some reason gevent doesn't work without gunicorn and eventlet wouldn't work with zerorpc..
I am building a web interface/data API using Flask and Flask-SocketIO for websocket communication. I would like to start shifting to a more development-ready setup using Gevent/Gevent-websocket, Gunicorn, and eventually Nginx for load balancing. However, after installing Gevent and Gevent-websocket, I am still getting a warning message when starting the SocketIO server:
WebSocket transport not available. Install eventlet or gevent and gevent-websocket for improved performance.
According to the Flask-SocketIO docs,
When the application is in debug mode the Werkzeug development server is still used and configured properly inside socketio.run(). In production mode the eventlet web server is used if available, else the gevent web server is used. If eventlet and gevent are not installed, the Werkzeug development web server is used.
This implies that the use of Gevent should be automated behind the scenes as part of Flask-SocketIO. I checked my Python installs with pip list and confirmed that I have Gevent 1.3.4 and Gevent-websocket 0.10.1 installed. Here is the initialization code for the SocketIO server:
app.py
flaskApp = Flask(__name__)
flaskApp.config['SESSION_TYPE'] = 'filesystem'
Session(flaskApp)
socketio = SocketIO(flaskApp, async_mode='threading', manage_session=False)
def createApp():
flaskApp.secret_key = "super secret"
socketio.run(flaskApp, host='0.0.0.0', port=80)
start.py
app.register_blueprint(monitor.blueprint)
...
createApp()
Why is Flask-SocketIO not detecting my Gevent install?
The portion of the docs that you quoted refers to the async_mode argument, and how it is set by default. You are setting async_mode='threading', so that disables the automatic selection of an async mode. Remove the argument, and then you'll get eventlet or gevent, depending on what you have installed.
I'm attempting to debug a Flask/Python app running on Google Appengine flexible environment.
However, I see a warning message within the Stackdriver Debug interface in Google Console, and am unable to set any breakpoints.
The warning reads:
Stackdriver Debugger is not set up for the python runtime on GAE Flex
Screenshot of warning
Any thoughts on what I'm doing wrong?
I've:
Enabled the Stackdriver Debugger API (as mentioned here)
Imported and initialised the Debugger (following the instructions here)
Included google-python-cloud-debugger in requirements.txt
main.py (the app entry point defined in app.yaml)
from werkzeug.serving import run_simple
from werkzeug.wsgi import DispatcherMiddleware
from wsgi import api, frontend, manage
try:
import googleclouddebugger
googleclouddebugger.AttachDebugger()
except ImportError:
pass
app = DispatcherMiddleware(frontend.create_app(), {
'/api': api.create_app(),
'/manage': manage.create_app()
})
if __name__ == '__main__':
run_simple('0.0.0.0', 5000, app, use_reloader=True, use_debugger=True)
app.yaml
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app
runtime_config:
python_version: 2
manual_scaling:
instances: 1
resources:
cpu: 1
memory_gb: 0.5
disk_size_gb: 10
env_variables:
SQLALCHEMY_DATABASE_URI: "postgresql+psycopg2://myusername:mypassword!#/instancename?host=/cloudsql/instancename"
beta_settings:
cloud_sql_instances: "instancename"
Update 1
After a comment and noticing a urllib import error, I wondered if the wsgi nature of my application was causing issues. I went back to the documentation, saw a note about Django framework doing something similar and changed the following:
googleclouddebugger.AttachDebugger()
to
googleclouddebugger.enable()
This got rid of the urllib import error, but hasn't resolved the overall issue.
I don't see anything wrong with this configuration, but here are some suggestions:
Once you open the GCP Console Debug page, please make sure you selected the correct version of the app from the top dropdown menu before starting the debugging session. It may be pointing to an old app version that is not debuggable, or the page and the dropdown might need a refresh to display the latest version.
Similarly, please check if your Stackdriver stderr logs contains a line that like this:
2017-12-24 15:14:14.000 PST I1224 23:14:14.600462 12 gcp_hub_client.py:335] Debuggee registered successfully, ID: gcp:1025611681465:7144dac417e43025
This means the debugger is actually setup/working properly, and most likely indicates that the UI is requires a refresh or selecting the right app version from the dropdown.
Make sure you have the google-python-cloud-debugger in the requirements.txt file.
Consider adding a simple print statement in the try/except block for the debugger, as folllows:
...
except ImportError as e:
print 'Failed to import Google Cloud Debugger: %s' % str(e)
pass
Then, check the stderr logs on Stackdriver Logging to see if this printed any exceptions.
I don't know about werkzeug, but you might want to try disabling the default debugger there by passing use_debugger=False, just to avoid possible conflicts.
Try deploying a simpler flask app (e.g., start with the hello world for Flex from here but modify it to use python_version: 2) and see if the debugger works for you there.
My web application uses the Pyramid framework, and runs on a Debian Linux system. I'm adding python-memcached to the application but cannot get the objects to be stored and retrieved. I get a null value when I retrieve a object from memcached using the key I used to set it with. The testing/debugging server I am using is the Pyramid Framework pserve server.
import memcache
mc = memcache.Client(['127.0.0.1:6543'], debug=0)
mc.set('key1', 'value1', 10)
val = mc.get('key1')
The val is equal to 'null'.
The command I use to run the application is:
$ pserve development.ini --reload
I doubt your memcache server is being run on port 6543 -- assuming you're using the default pyramid config file, your development server is running on port 6543, your memcache server is probably on port 11211. Try running the memcache server and then set
mc = memcache.Client(['127.0.0.1:11211'], debug=0)