简体   繁体   中英

Trying to deploy a Flask app on CherryPy server

I was trying to deploy my Flask app on CherryPy server. I liked its simplistic and minimalistic nature.

So I PIP'ed CherryPy like below

pip install CherryPy-15.0.0-py2.py3-none-any.whl

and wrote script like below -very common suggested by many sources

from cherrypy import wsgiserver
from hello import app

d = wsgiserver.WSGIPathInfoDispatcher({'/': app})
server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 80), d)

if __name__ == '__main__':
   try:
      server.start()
   except KeyboardInterrupt:
      server.stop()

To my surprise, I had imports errors. After a few googling around, I learned that I had to change my import lines to cheroot to make it work.

from cheroot.wsgi import Server
from cheroot.wsgi import PathInfoDispatcher

Now, my code is working fine. However, I am a bit confused if this is the right way of using CherryPy WSGI server or if I pip'ed a wrong version of CherryPy. I am confused because Cheroot seems to be more than year old (dates all the way back to 2014), yet all the information I found around Flask on CherryPy WSGI server is using from cherrypy import wsgiserver , not from cheroot.wsgi import Server , even the latest postings.

This makes me unsure if I am doing the right thing or not.

Can someone please shed light on this confusion?

Cheroot ( src ) is a low-level HTTP and WSGI server, which used to be a part of CherryPy ( src ) once, but has been factored out into a separate repo a while back. So former cherrypy.wsgiserver has moved to cheroot.wsgi module.

It's completely replaceable and designed to allow developers to depend on Cheroot directly if they only use WSGI server, not requiring other parts of CherryPy.

So here's how you can use it in a version-agnostic way:

try:
    from cheroot.wsgi import Server as WSGIServer, PathInfoDispatcher
except ImportError:
    from cherrypy.wsgiserver import CherryPyWSGIServer as WSGIServer, WSGIPathInfoDispatcher as PathInfoDispatcher

from hello import app

d = PathInfoDispatcher({'/': app})
server = WSGIServer(('0.0.0.0', 80), d)

if __name__ == '__main__':
   try:
      server.start()
   except KeyboardInterrupt:
      server.stop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM