溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python?web.py怎么啟動https端口

發(fā)布時間:2023-05-04 15:41:28 來源:億速云 閱讀:131 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“python web.py怎么啟動https端口”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“python web.py怎么啟動https端口”吧!

python web.py啟動https端口

        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

        示例程序如下所示:

# -*- 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': '系統(tǒng)正常!'}     
    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)

補充:python web.py 開啟https

第一步:在shell中依次執(zhí)行以下命令,回答問題,設(shè)置密碼生成證書,包含三個文件***.crt 和***.key和***.csr,我分別重新命令為server.crt  server.csr  server.key

 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

開啟服務(wù),仿照下面代碼輸入即可,其中

from handle import Handle引入的我的方法模塊,在urls中調(diào)用(仿照微信公眾號開發(fā)的例子,具體如果新手可以自己看),同時保存的文件路徑根據(jù)自己的寫

import web
from handle import Handle
from web.wsgiserver import CherryPyWSGIServer
CherryPyWSGIServer.ssl_certificate = "/usr/ssl/server.crt"
CherryPyWSGIServer.ssl_private_key = "/usr/ssl/server.key"
urls = (
    '/wx', 'Handle',
)
if __name__ == '__main__':
    app = web.application(urls, globals())
    app.run()

然后開始服務(wù) sudo python main.py 443(其中443是端口號)

這個時候你需要輸入ssl之前自己設(shè)置的密碼,才能開啟,但是這樣導致不能后臺隱藏,

但是在生成證書的文件夾下,執(zhí)行sudo openssl rsa -in server.key -out server.key即可無密碼,這樣就可以后臺執(zhí)行

nohup python main.py 443 &

到此,相信大家對“python web.py怎么啟動https端口”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI