筆者著嘗試在IIS中部署Cherrypy,心得如下
- 為IIS啓用CGI功能
- 在命令行執行
pip install wfastcgi
- 在命令行執行
wfastcgi-enable
(需要管理員權限) - 在IIS中配置Handler Mapping,參考以下
web.config
,注意<add key="WSGI_HANDLER" value="app.wsgiapp" />
中的value對應你app.py裏面的wsgiapp
-
pip install cherrypy
,編寫app.py代碼,參考以下app.py
- 瀏覽 http://localhost/cherrypy 或 http://localhost/cherrypy/greet/peter 即可看到輸出結果
如有問題,請留言
網站結構
DefaultSite
|-cherrypy
| |-app.py
| |-web.config
|-flask
web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="Python FastCGI - Cheerypy"
path="*"
verb="*"
modules="FastCgiModule"
scriptProcessor="C:\Python36\python.exe|C:\PythonWeb\cherrypy\wfastcgi.py"
resourceType="Unspecified"
requireAccess="Script" />
</handlers>
</system.webServer>
<appSettings>
<!-- Required settings -->
<add key="WSGI_HANDLER" value="app.wsgiapp" />
<add key="PYTHONPATH" value="C:\PythonWeb\Cherrypy" />
<!-- Optional settings -->
<!--
<add key="WSGI_LOG" value="C:\Logs\my_app.log" />
<add key="WSGI_RESTART_FILE_REGEX" value=".*((\.py)|(\.config))$" />
<add key="APPINSIGHTS_INSTRUMENTATIONKEY" value="__instrumentation_key__" />
<add key="DJANGO_SETTINGS_MODULE" value="my_app.settings" />
<add key="WSGI_PTVSD_SECRET" value="__secret_code__" />
<add key="WSGI_PTVSD_ADDRESS" value="ipaddress:port" />
-->
</appSettings>
</configuration>
app.py
import cherrypy
class Root:
@cherrypy.expose
def index(self):
return 'Hello CherryPy!'
@cherrypy.expose
def greet(self, name):
return 'Greetings, {0}'.format(name)
url_prefix = '/cherrypy'
cherrypy.config.update({'engine.autoreload.on': False})
cherrypy.server.unsubscribe()
cherrypy.engine.start()
wsgiapp = cherrypy.tree.mount(Root(), url_prefix)