- CherryPy 教程
- CherryPy - 首页
- CherryPy - 简介
- CherryPy - 环境搭建
- CherryPy - 术语表
- 内置HTTP服务器
- CherryPy - 工具箱
- CherryPy - 一个可运行的应用
- CherryPy - Web 服务
- CherryPy - 表现层
- CherryPy - Ajax 的使用
- CherryPy - 演示应用
- CherryPy - 测试
- 应用部署
- CherryPy 有用资源
- CherryPy - 快速指南
- CherryPy - 有用资源
- CherryPy - 讨论
CherryPy - 应用部署
本章将重点介绍基于 CherryPy 的应用,通过内置的 CherryPy HTTP 服务器启用 SSL。
配置
Web 应用需要不同级别的配置设置:
Web 服务器 - 与 HTTP 服务器相关的设置
引擎 - 与主机引擎相关的设置
应用 - 用户使用的应用
部署
CherryPy 应用的部署被认为是一种相当简单的方法,所有需要的包都可以在 Python 系统路径中找到。在共享的 Web 托管环境中,Web 服务器将位于前端,允许主机提供商执行过滤操作。前端服务器可以是 Apache 或lighttpd。
本节将介绍一些在 Apache 和 lighttpd Web 服务器后运行 CherryPy 应用的解决方案。
cherrypy
def setup_app():
class Root:
@cherrypy.expose
def index(self):
# Return the hostname used by CherryPy and the remote
# caller IP address
return "Hello there %s from IP: %s " %
(cherrypy.request.base, cherrypy.request.remote.ip)
cherrypy.config.update({'server.socket_port': 9091,
'environment': 'production',
'log.screen': False,
'show_tracebacks': False})
cherrypy.tree.mount(Root())
if __name__ == '__main__':
setup_app()
cherrypy.server.quickstart()
cherrypy.engine.start()
SSL
SSL(安全套接字层)可以在基于 CherryPy 的应用中得到支持。要启用 SSL 支持,必须满足以下要求:
- 在用户的环境中安装 PyOpenSSL 包
- 在服务器上拥有 SSL 证书和私钥
创建证书和私钥
让我们处理证书和私钥的要求:
- 首先,用户需要一个私钥:
openssl genrsa -out server.key 2048
- 此密钥不受密码保护,因此保护较弱。
- 将发出以下命令:
openssl genrsa -des3 -out server.key 2048
程序将需要一个密码短语。如果您的 OpenSSL 版本允许您提供空字符串,请这样做。否则,输入默认密码短语,然后按照如下步骤将其从生成的密钥中删除:
openssl rsa -in server.key -out server.key
- 证书的创建如下:
openssl req -new -key server.key -out server.csr
此过程将要求您输入一些详细信息。为此,必须发出以下命令:
openssl x509 -req -days 60 -in server.csr -signkey server.key -out server.crt
新签发的证书有效期为 60 天。
以下代码显示了它的实现:
import cherrypy
import os, os.path
localDir = os.path.abspath(os.path.dirname(__file__))
CA = os.path.join(localDir, 'server.crt')
KEY = os.path.join(localDir, 'server.key')
def setup_server():
class Root:
@cherrypy.expose
def index(self):
return "Hello there!"
cherrypy.tree.mount(Root())
if __name__ == '__main__':
setup_server()
cherrypy.config.update({'server.socket_port': 8443,
'environment': 'production',
'log.screen': True,
'server.ssl_certificate': CA,
'server.ssl_private_key': KEY})
cherrypy.server.quickstart()
cherrypy.engine.start()
下一步是启动服务器;如果成功,您将在屏幕上看到以下消息:
HTTP Serving HTTPS on https://:8443/
广告