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

相关推荐
浩浩测试一下4 小时前
Linux 底层知识点 1
linux·运维·服务器
Eloudy4 小时前
全文 - IMEX 第01节 -IMEX overview
运维·服务器·网络
Escalating_xu5 小时前
【Linux 网络编程】TCP 跨平台实战:用 Windows 客户端访问 Linux 服务器
linux·服务器·网络
发量惊人的中年网工5 小时前
分公司访问总部ERP慢怎么办?SD-WAN组网的排查与验收要点
服务器·开发语言·php
翼龙云_cloud5 小时前
亚马逊云服务器服务代理商:Bedrock 实践经验 Serverless 架构下的 AI 自动化落地
服务器·架构·serverless·云计算·aws
忆挽篱笙歌6 小时前
环境变量,本地变量
linux·运维·服务器
小天源8 小时前
让访问数据可解释:IP 地球大屏、地区库更新与运行维护设计
网络·网络协议·tcp/ip
张宇Joaquin8 小时前
AMD uProf 工具采集微架构特征使用介绍
服务器
老李IT笔记8 小时前
连不上不一定是网不好:新系统不认的服务端配置卡在哪一档
运维·服务器·网络
万年咸鱼9 小时前
WebSocket、Socket 与 HTTP 的区别详解
websocket·网络协议·http