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

相关推荐
2501_9160074719 分钟前
没有 Mac,我如何用 Appuploader 完成 iOS App 上架
websocket·网络协议·tcp/ip·http·网络安全·https·udp
2501_9160137424 分钟前
App 发布后才想起安全?iOS 后置混淆的实战方法与工具路线(含 Ipa Guard 应用体验)
websocket·网络协议·tcp/ip·http·网络安全·https·udp
小峰编程27 分钟前
Python函数——万字详解
linux·运维·服务器·开发语言·前端·网络·python
用手码出世界1 小时前
传输层协议UDP
网络·网络协议·udp
厦门辰迈智慧科技有限公司1 小时前
大中型水闸安全监测系统解决方案
运维·服务器·安全
weixin_449568702 小时前
Nginx端口telnet不通排查指南
运维·服务器·nginx
一袋米扛几楼982 小时前
【网络】Wireshark练习3 analyse DNS||ICMP and response message
服务器·网络·wireshark
五花肉村长2 小时前
Linux-进程信号
linux·运维·服务器·开发语言·网络·c++
无锡耐特森2 小时前
ModbusTCP转 Profinet网关:热收缩包装机智能化改造核心方案
运维·网络·网络协议·自动化
tmacfrank2 小时前
HTTP 协议基础
网络·网络协议·http