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

相关推荐
xlq223224 分钟前
30.进程池IPC
linux·运维·服务器
Predestination王瀞潞17 分钟前
5.2.1 通信->DNS域名系统协议标准(IETF RFC 1035):DNS(Domain Name System)
网络·网络协议·tcp/ip
nuomigege18 分钟前
beagleboneblack刷入官方IOT镜像后无法运行nodered问题的处理
linux·运维·服务器
落叶花开又一年1 小时前
检验检测机构资质认定远程评审工作程序
linux·运维·服务器
wanhengidc1 小时前
《三国志异闻录》搬砖新游戏 云手机
运维·服务器·数据库·游戏·智能手机
旺仔.2912 小时前
僵死进程及Linux文件操作 详解
linux·运维·服务器
于慨3 小时前
tauri
java·服务器·前端
riyue6663 小时前
封装 WebSocket 工具类
网络·vue.js·websocket·网络协议·v
十巷无终3 小时前
Kali Virtual Machines(虚拟机镜像)安装后问题及解决办法
linux·运维·服务器
你有按下913的勇气吗3 小时前
【Agent,RAG,Transform】
linux·运维·服务器