web.py启动https端口需要ssl证书,如果没有ssl证书,那么可以通过如下方式生成。具体可参考“https://blog.youkuaiyun.com/FinalDragonborn/article/details/79301026”。
openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
sudo openssl rsa -in server.key -out server.key
示例程序如下所示:
# -*- coding: utf-8 -*-
"""
Created on Mon May 10 20:37:00 2021
@author: Administrator
"""
import web #web.py
urls = (
'/server' , 'server',
'/.*', 'notfound' #localhost:port/其他任意界面,访问notfound类
)
class MyApplication(web.application):
def run(self, port=8080, *middleware):
func = self.wsgifunc(*middleware)
return web.httpserver.runsimple(func, ('0.0.0.0', port))
class server:
def __init__(self):
self.return_msg = {'errorCode': 0, 'msg': '系统正常!'}
def POST(self): #POST处理方式与GET一致
# content = web.input()
# print('收到消息:', content.key1, content.key2, content.key3)
x = web.input(myfile={})
print('xxx: ', x.keys())
return str(self.return_msg).replace('\'', '\"')
class notfound:
def GET(self):
print('--from notfound')
return '404 not found'
def POST(self):
print('--from notfound')
return '404 not found'
from cheroot.server import HTTPServer
from cheroot.ssl.builtin import BuiltinSSLAdapter
HTTPServer.ssl_adapter = BuiltinSSLAdapter(
certificate='server.crt',
private_key='server.key')
if __name__ == "__main__":
app = MyApplication(urls ,globals())
app.run(port=443)