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

相关推荐
fiveym6 小时前
01 - iPXE + Clonezilla 网络装机原理解析
linux·运维·服务器·网络
虎头金猫6 小时前
如何在群晖NAS上通过Docker部署CloudSaver?群晖部署CloudSaver教程|聚合资源搜索并实现远程访问
运维·服务器·网络·python·docker·容器·pandas
dog2506 小时前
网络优化,还是降低各阶矩
服务器·网络·php
sudebao点com8 小时前
一次“Google 能打开,FlexTV 打不开”的 DNS 故障排查:从 curl 超时到完整证据链
windows·macos·https·cdn·dns·nslookup·网络排错
非凡ghost11 小时前
分区助手:Windows 磁盘管理的“瑞士军刀“,分区调整不再怕丢数据
服务器·windows·电脑·软件需求
许彰午11 小时前
# allbytaskid——一个接口扛六个DataStore
运维·服务器·php
吠品11 小时前
Java byte数组与String互转:编码细节与踩坑记录
java·linux·服务器
木白CPP12 小时前
Linux 驱动UART子系统源码分析
linux·运维·服务器·驱动开发·嵌入式硬件·开源软件
Titan202414 小时前
Linux网络基础知识
linux·服务器·网络·c++·学习
Titan202415 小时前
Linux网络学习:套接字、UDP的封装与应用
linux·服务器·开发语言·网络·c++