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

相关推荐
饼瑶5 分钟前
Isaac Sim 5.1.0 部署指南(实验室服务器)
服务器·仿真·具身智能
fetasty12 分钟前
chroot的Linux服务配置-当云服务器真正用起来
android·linux·服务器
云飞云共享云桌面27 分钟前
研发部门使用SolidWorks和ug,cad,设计共享云桌面应该怎么选?
运维·服务器·网络·人工智能·3d
AI_Claude_code31 分钟前
ZLibrary访问困境方案二:DNS-over-HTTPS/TLS配置与隐私保护实践
爬虫·python·网络协议·http·网络安全·https·网络爬虫
亚空间仓鼠34 分钟前
OpenEuler系统常用服务(十)
linux·运维·服务器·网络
艾莉丝努力练剑34 分钟前
【Linux线程】Linux系统多线程(四):线程ID及进程地址空间布局,线程封装
java·linux·运维·服务器·c语言·c++·学习
同聘云34 分钟前
阿里云国际站 服务器的“客户端” (Client) 和 “服务器” (Server)到底是什么?有什么区别?
服务器·网络·阿里云·云计算·云小强
邓霖涛35 分钟前
nginx使用openSSL自签生成https相关证书
服务器·nginx·https
程序员老邢1 小时前
【技术底稿 10】16G Ubuntu 服务器手动部署 Ollama 0.20.4 全流程(避坑 HTTP2 错误)
服务器·ubuntu·ai·语言模型·devops
饼瑶1 小时前
Isaac Sim 5.0.0 Docker 部署手册(实验室服务器)
服务器·docker·容器