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

相关推荐
佑白雪乐7 小时前
<Linux基础第10集>复习前面内容
linux·运维·服务器
春日见7 小时前
自动驾驶规划控制决策知识点扫盲
linux·运维·服务器·人工智能·机器学习·自动驾驶
landonVM9 小时前
Linux 上搭建 Web 服务器
linux·服务器·前端
学习中的DGR9 小时前
[极客大挑战 2019]Http 1 新手解题过程
网络·python·网络协议·安全·http
云游云记9 小时前
nesbot/carbon 常用功能总结
linux·运维·服务器
landonVM9 小时前
Linux 下的高效压缩工具 Zstandard
linux·运维·服务器
遇见火星9 小时前
服务器运维操作命令速查手册
运维·服务器
EmbedLinX10 小时前
Linux之内存管理
linux·服务器·c语言·c++
2301_7925800010 小时前
xuepso
java·服务器·前端
Nightwish510 小时前
Linux随记(二十八)
linux·运维·服务器