python秒起https文件服务器

简介: python秒起https 文件服务器

python秒起https 文件服务器

前几天博客有个秒级启动http web服务器:
python -m http.server 6666
结果有同事想要求换成https web服务器,所以就有了下文

文章在这里:

python实现秒级启动http、ftp服务器

一、windows版本:

1.安装openssl

openssl官方下载地址

下载msi版本,一路下一步,最后一步全部取消勾选,这里有坑

配置环境变量就和python一样了

image-1660751630636

image-1660751515268

第二天我会上传到工作群,openssl安装包

2.生成证书

openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem

image-1660750829316

image-1660751749551

3.启动https服务

# coding=utf-8
"""
    @Project :pachong-master 
    @File    :httpserver.py
    @Author  :gaojs
    @Date    :2022/8/17 22:29
    @Blogs   : https://www.gaojs.com.cn
"""

import http.server
import ssl


def https_web_server():
    """
    https服务器
    :return:
    """
    server_ip = 'localhost'
    # 这里port不要写成字符串,我刚开始给成字符串,报错搞了好一会
    server_port = 5001
    server_address = (server_ip, server_port)
    # 生成证书步骤:
    # openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem
    server_cert = "./cert.pem"
    server_key = "./key.pem"

    httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler)
    httpd.socket = ssl.wrap_socket(
        httpd.socket,
        server_side=True,
        certfile=server_cert,
        keyfile=server_key,
        ssl_version=ssl.PROTOCOL_TLS)

    print("Server HTTPS on " + server_ip + " port " + str(server_port) + " (https://" + server_ip + ":" + str(server_port) + ") ... ")
    httpd.serve_forever()


if __name__ == '__main__':
    https_web_server()

4.结果如下

image-1660750871759

image-1660750903293

二、linux版本

1.生成证书

我这里使用的是阿里云的镜像,所以默认自带openssl

openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem

2.启动https服务器

# coding=utf-8
"""
    @Project :pachong-master 
    @File    :httpserver.py
    @Author  :gaojs
    @Date    :2022/8/17 22:29
    @Blogs   : https://www.gaojs.com.cn
"""

import http.server
import ssl


def https_web_server():
    """
    https服务器
    :return:
    """
    server_ip = 'localhost'
    server_port = 5001
    server_address = (server_ip, server_port)
    # 生成证书步骤:
    # openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem
    server_cert = "./cert.pem"
    server_key = "./key.pem"

    httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler)
    httpd.socket = ssl.wrap_socket(
        httpd.socket,
        server_side=True,
        certfile=server_cert,
        keyfile=server_key,
        ssl_version=ssl.PROTOCOL_TLS)

    print("Server HTTPS on " + server_ip + " port " + str(server_port) + " (https://" + server_ip + ":" + str(server_port) + ") ... ")
    httpd.serve_forever()


if __name__ == '__main__':
    https_web_server()

三、加入腾讯云自媒体分享计划

我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=1otwwvb9ht470

相关文章
|
1月前
HAProxy的高级配置选项-配置haproxy支持https协议及服务器动态上下线
文章介绍了如何配置HAProxy以支持HTTPS协议和实现服务器的动态上下线。
80 8
HAProxy的高级配置选项-配置haproxy支持https协议及服务器动态上下线
|
23天前
|
安全 Python
使用Python实现简单的Web服务器
使用Python实现简单的Web服务器
22 6
|
2月前
|
运维 监控 数据库
自动化运维:使用Python脚本实现服务器监控
【8月更文挑战第31天】在这篇文章中,我们将探索如何利用Python编写简单的脚本来实现对服务器的基本监控。通过学习和应用这些技术,你可以快速检测服务器的状态,包括CPU使用率、内存占用和磁盘空间等关键指标。这不仅有助于及时发现问题,还能提升运维效率。文章将逐步引导你理解监控的重要性,并展示如何从零开始构建自己的监控工具。
|
2月前
|
Shell 网络安全 数据安全/隐私保护
使用 Python 远程登陆服务器的最佳实践
使用 Python 远程登陆服务器的最佳实践
|
2月前
|
网络协议 安全 Unix
6! 用Python脚本演示TCP 服务器与客户端通信过程!
6! 用Python脚本演示TCP 服务器与客户端通信过程!
|
2月前
|
存储 运维 监控
自动化运维:使用Python脚本进行服务器监控
【8月更文挑战第31天】在数字化时代,服务器的稳定运行对于企业至关重要。本文将介绍如何使用Python编写一个简单的服务器监控脚本,帮助运维人员及时发现并解决潜在问题。我们将从基础的服务器资源监控开始,逐步深入到日志分析与报警机制的实现。通过实际代码示例和操作步骤,使读者能够快速掌握自动化监控的技能,提升工作效率。
|
2月前
|
数据可视化 Python
通过python建立一个web服务查看服务器上的文本、图片、视频等文件
通过python建立一个web服务查看服务器上的文本、图片、视频等文件
28 0
|
2月前
|
网络协议 API 网络安全
Python远程连接服务器用它就够了
Python远程连接服务器用它就够了
|
2月前
|
API 开发工具 Python
【Azure Developer】使用 Azure Python SDK时,遇见 The resource principal named https://management.azure.com was not found in the tenant China Azure问题的解决办法
【Azure Developer】使用 Azure Python SDK时,遇见 The resource principal named https://management.azure.com was not found in the tenant China Azure问题的解决办法
|
2月前
|
Linux Python
【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https
【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https
下一篇
无影云桌面