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

相关推荐
4t4run6 小时前
28、Linux 系统定时任务
linux·运维·服务器
cui__OaO6 小时前
Linux驱动--基于驱动设备分离的按键中断驱动
linux·运维·服务器·嵌入式
数字芯片实验室6 小时前
IP验证最终回归到时序级建模
网络·网络协议·tcp/ip·fpga开发
OnlyEasyCode7 小时前
Linux下载Navicat、特定版本Mysql
linux·运维·服务器
济6177 小时前
linux 系统移植(第七期)----U-Boot 图形化配置及其原理-- Ubuntu20.04
linux·运维·服务器
米高梅狮子8 小时前
01. 配置DHCP服务器
服务器·网络·php
馨谙8 小时前
Linux面试题----文件权限,chmod,chown,suid,sgid,粘滞位,umask
linux·运维·服务器
青衫客369 小时前
Linux 磁盘挂载全流程实战——从新盘初始化到安全开机自启
linux·运维·服务器
掘根9 小时前
【jsonRpc项目】常用的零碎功能接口实现
网络协议·http
小周学学学10 小时前
ESXI故障处理-重启后数据存储丢失
linux·运维·服务器