web.py啟動https連接埠需要ssl證書,如果沒有ssl證書,那麼可以透過以下方式產生。
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
卷口密碼產生證書,包含三個檔案***.crt 和***.key和***.csr,我分別重新指令為server.crt server.csr 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)
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 mv server.key myserver.key mv server.crt myserver.crt
然後開始服務sudo python main.py 443(其中443是連接埠號碼)這個時候你需要輸入ssl之前自己設定的密碼,才能開啟,但是這樣導致不能後台隱藏,
但是在生成證書的資料夾下,執行sudo openssl rsa -in server.key -out server.key即可無密碼,這樣就可以後台執行
nohup python main.py 443 &
以上是python web.py怎麼啟動https埠的詳細內容。更多資訊請關注PHP中文網其他相關文章!