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

相关推荐
ipooipoo118812 小时前
如何判断动态IP池里的IP都是纯净的吗?
服务器·网络·tcp/ip
鱼大虾13 小时前
centos服务器安装gitlab
服务器·centos·gitlab
游戏开发爱好者813 小时前
2025年iOS应用上架App Store全指南,开发者必看
android·ios·小程序·https·uni-app·iphone·webview
赋创小助手13 小时前
超微 SYS-E403-14B-FRN2T 深度解析:面向边缘与 IoT 场景的高扩展紧凑型服务器
运维·服务器·人工智能·科技·物联网·ai·边缘计算
澄澈青空~13 小时前
病毒木马侵入系统内核的底层运作机理
java·linux·服务器
Htojk13 小时前
openssl签发自签名证书的流程
网络协议·https·ssl
AI+程序员在路上13 小时前
嵌入式Linux中添加ftp服务器的简易方法
linux·运维·服务器
小码吃趴菜13 小时前
TCP协议编程流程
服务器·网络·tcp/ip
北京耐用通信13 小时前
如何用耐达讯自动化Profibus总线光纤中继器解决变频器长距离通信干扰问题?
人工智能·物联网·网络协议·自动化·信息与通信
阿里超级工程师14 小时前
yunedit-ssh相比jenkins和winscp的特点和优势分析
服务器·ssh·jenkins