python web.py启动https端口的方式

作者:Coding的叶子 时间:2021-10-20 11:33:44 

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': '系统正常!'}    
   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

参考英文网址http://heapkeeper-heap.github.io/hh/thread_1344.html

第一步:在shell中依次执行以下命令,回答问题,设置密码生成证书,包含三个文件***.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

开启服务,仿照下面代码输入即可,其中

from handle import Handle引入的我的方法模块,在urls中调用(仿照微信公众号开发的例子,具体如果新手可以自己看),同时保存的文件路径根据自己的写

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()

然后开始服务 sudo python main.py 443(其中443是端口号)

这个时候你需要输入ssl之前自己设置的密码,才能开启,但是这样导致不能后台隐藏,

但是在生成证书的文件夹下,执行sudo openssl rsa -in server.key -out server.key即可无密码,这样就可以后台执行

nohup python main.py 443 &

来源:https://blog.csdn.net/suiyingy/article/details/128641728

标签:python,web.py,https,端口
0
投稿

猜你喜欢

  • Python基于SMTP协议实现发送邮件功能详解

    2022-07-17 00:31:00
  • Oracle系统表外键的更名

    2010-07-26 13:07:00
  • border边框在浏览器中的渲染方式

    2010-03-10 10:52:00
  • 一直闪烁变色的超级链接代码

    2008-02-27 13:08:00
  • Python创建多线程的两种常用方法总结

    2023-11-16 16:41:09
  • 从JavaScript的函数重名看其初始化方式

    2023-07-16 22:00:12
  • Oracle数据安全面面观

    2010-07-27 13:27:00
  • Python3如何在服务器打印资产信息

    2023-11-15 08:03:45
  • python实现维吉尼亚算法

    2023-07-02 01:28:17
  • Python分析彩票记录并预测中奖号码过程详解

    2023-07-20 04:49:18
  • asp之自动闭合HTML/ubb标签函数 附简单注释

    2011-02-28 11:24:00
  • Python实现爬虫设置代理IP和伪装成浏览器的方法分享

    2021-05-26 19:42:29
  • django+celery+RabbitMQ自定义多个消息队列的实现

    2021-01-21 16:52:10
  • Python实现合并两个有序链表的方法示例

    2023-04-02 22:20:46
  • node.js+express+mySQL+ejs+bootstrop实现网站登录注册功能

    2023-07-15 17:55:01
  • Python3安装Pymongo详细步骤

    2021-06-09 10:27:20
  • Python3使用SMTP发送带附件邮件

    2022-02-10 12:05:53
  • asp下用fso和ado.stream写xml文件的方法

    2011-04-07 10:55:00
  • Python模拟键盘输入自动登录TGP

    2021-11-04 11:27:22
  • 带你深入了解Access数据库的4种安全方式

    2008-11-28 14:34:00
  • asp之家 网络编程 m.aspxhome.com