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

相关推荐
檀越剑指大厂30 分钟前
【Linux系列】如何在 Linux 服务器上快速获取公网
linux·服务器·php
共享家952732 分钟前
linux-高级IO(上)
java·linux·服务器
小浣浣2 小时前
为何她总在关键时“失联”?—— 解密 TCP 连接异常中断
网络·网络协议·tcp/ip
曳渔2 小时前
UDP/TCP套接字编程简单实战指南
java·开发语言·网络·网络协议·tcp/ip·udp
HWL56792 小时前
“preinstall“: “npx only-allow pnpm“
运维·服务器·前端·javascript·vue.js
Xの哲學4 小时前
Perf使用详解
linux·网络·网络协议·算法·架构
门前灯4 小时前
Linux系统之iprconfig 命令详解
linux·运维·服务器·iprconfig
忧郁的橙子.4 小时前
三、k8s 1.29 之 安装2
linux·运维·服务器
huangyuchi.5 小时前
【Linux系统】动静态库的制作
linux·运维·服务器·动态库·静态库·库的简单制作
闻不多5 小时前
用llamaindex搭建GAR遇到400
android·运维·服务器