python快速搭建https服务器

本文介绍了在ubuntu操作系统上搭建https服务器的过程

在一台连接到网络的主机上搭建https服务器,假设该主机的ip地址为:10.98.69.174

创建证书example.crt和私钥example.key

bash 复制代码
openssl req -newkey rsa:2048 -nodes -keyout example.key -x509 -days 365 -out example.crt

使用命令可以查看证书详情

bash 复制代码
openssl x509 -in example.crt -text -noout

编辑python脚本文件https_server.py

python 复制代码
import ssl
from http.server import HTTPServer, BaseHTTPRequestHandler

class MyRequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        pass

if __name__ == '__main__':
    context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
    context.load_cert_chain(certfile='./example.crt', keyfile="./example.key")

    ciphers = 'ECDHE-ECDSA-AES128-GCM-SHA256:TLS_CHACHA20_POLY1305_SHA256...'
    context.set_ciphers(ciphers)

    ciphers_suit = context.get_ciphers()
    for i in range(len(ciphers_suit)):
        print(f"{i}: {ciphers_suit[i]['name']}")

    httpd = HTTPServer(('0.0.0.0', 4443), MyRequestHandler)
    httpd.socket = context.wrap_socket(httpd.socket, server_side=True)
    httpd.serve_forever()

然后运行python脚本

bash 复制代码
python3 https_server.py

可以在局域网内通过火狐浏览器访问 https://10.98.69.174:4443 查看https服务器是否已经生效

本文在写作中,以下文章给作者带来了很大的帮助,特此表示感谢

快速架设Python HTTPS服务 - 又是火星人 - 博客园

相关推荐
2501_916007478 分钟前
iOS 应用上架流程的工程化拆解 从签名体系到提交审核的全过程管控
android·ios·小程序·https·uni-app·iphone·webview
还不秃顶的计科生8 分钟前
“please restart word to load mathtype“解决方法
运维·服务器
傻啦嘿哟13 分钟前
Python文件目录比较全攻略:从基础到实战
服务器·前端·数据库
2501_9159184114 分钟前
构建可靠的 iOS 日志导出体系,从真机日志到系统行为的多工具协同实践
android·ios·小程序·https·uni-app·iphone·webview
YANshangqian16 分钟前
微软Microsoft Edge浏览器 v143.0.3650.75 基于Chromium内核
linux·运维·服务器
从负无穷开始的三次元代码生活20 分钟前
《图解HTTP》——浓缩讲解,快速了解HTTP
网络·网络协议·http
Kaede627 分钟前
MCP支持的通信协议:stdio、SSE、HTTP
网络·网络协议·http
wanhengidc29 分钟前
服务器的安全保障如何
运维·服务器·安全·web安全·php
xjxijd31 分钟前
AWS Proton 2.0 实测:一键生成 CI/CD 流水线,云原生部署效率提 200%
服务器·aws
无奈笑天下9 小时前
银河麒麟高级服务器操作系统【双网卡绑定之bond0】操作方法
linux·运维·服务器·网络·经验分享