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服务 - 又是火星人 - 博客园

相关推荐
2301_7779983411 分钟前
TCP协议:滑动窗口与流量控制
服务器·网络·网络协议·tcp/ip·php
well06122 小时前
Linux 文件相关底层知识
linux·运维·服务器
Lsetea2 小时前
OpenSSL verify报CA certificate key too weak:auth_level与弱密钥链排查
运维·https·ssl证书·openssl·证书链
wangbing11252 小时前
开发指南148-WebSocket-前端查报文
网络·websocket·网络协议
库拉镜像AI牛牛2 小时前
漫剧工作室量产方案:依托知漫剧 AI 短剧降本增效
大数据·服务器·前端·人工智能·语音识别
沫璃染墨3 小时前
《从零入门Linux系统篇(五十八):线程篇·十一——线程安全与死锁详解:从可重入到多锁管理》
linux·服务器·开发语言·c++·驱动开发·安全·架构
姜鱼问生3 小时前
宝塔面板优雅下线:先 stop 再 disable 再 rm
运维·服务器
闭包不眠4 小时前
PDF文字提取交付前该检查什么
运维·服务器·图像处理·人工智能·计算机视觉·pdf
guo_wen_qiang4 小时前
云服务器sentinel搭建-控制台
服务器·sentinel
liangshanbo12154 小时前
前端高级面试题:WebSocket 双向流式通信怎么设计?
前端·websocket·网络协议