Python极速搭建Linux/Windows文件下载服务器:一行命令实现局域网文件共享

Python开启文件下载服务器

一、常用参数详解

1.端口号
# 使用 8080 端口 (windows直接cmd窗口python没有3)
python3 -m http.server 8080
  • 1.
  • 2.
2.--bind/-b(绑定地址)

指定服务器绑定的 IP 地址,默认 0.0.0.0(所有接口)。

# 仅允许本地访问:--bind 127.0.0.1
# 允许局域网访问:--bind 0.0.0.0

# 绑定到指定IP (windows直接cmd窗口python没有3)
python3 -m http.server -b 192.168.1.100
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
3.--directory/-d(服务目录)

指定服务器根目录(默认当前目录)。

# 共享 /data 目录
python3 -m http.server --directory /data  

# 如果是windows系统的话 共享D盘的data目录
python -m http.server -d D:\data
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
4.--cgi(启用 CGI 支持)

启动 CGI 服务器,可执行 .cgi脚本。

# 支持动态脚本 (windows直接cmd窗口python没有3)
python3 -m http.server --cgi
  • 1.
  • 2.

二、进阶用法

1.HTTPS 支持(需 OpenSSL)生成证书后启动 HTTPS 服务:

1)、Windows:

安装OpenSSL
https://slproweb.com/products/Win32OpenSSL.html
https://slproweb.com/download/Win64OpenSSL_Light-3_5_1.exe#下载完后,打开下一步直至安装完成
#打开CMD窗口
Microsoft Windows [版本 10.0.19044.5854]
(c) Microsoft Corporation。保留所有权利。

# 在D盘创建ssl目录
C:\Users\Administrator>mkdir D:\ssl

# 进入到openssl目录/bin目录下
C:\Users\Administrator>cd C:\Program Files\OpenSSL-Win64\bin

# 生成证书
C:\Program Files\OpenSSL-Win64\bin>openssl req -x509 -newkey rsa:2048 -nodes -keyout D:\ssl\server.key -out D:\ssl\server.crt -days 3650 -subj "/C=CN/ST=Beijing/L=Beijing/O=MyOrg/CN=localhost" -addext "subjectAltName=DNS:localhost,IP:127.0.0.1"

# 查看创建的证书
C:\Program Files\OpenSSL-Win64\bin>dir D:\ssl
 驱动器 D 中的卷是 新加卷
 卷的序列号是 64C3-7801

 D:\ssl 的目录

2025/07/24  14:12    <DIR>          .
2025/07/24  14:12    <DIR>          ..
2025/07/24  14:12             1,348 server.crt
2025/07/24  14:12             1,732 server.key
               2 个文件          3,080 字节
               2 个目录 204,763,607,040 可用字节
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
①、命令行
# cmd直接执行 
# 切换到共享目录
cd D:\data
# 执行
python -c "from http.server import HTTPServer, SimpleHTTPRequestHandler; import ssl; server = HTTPServer(('0.0.0.0', 443), SimpleHTTPRequestHandler); server.socket = ssl.wrap_socket(server.socket, keyfile='D:\ssl\server.key', certfile='D:\ssl\server.crt', server_side=True); print('HTTPS on :443'); server.serve_forever()"
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

浏览器访问:https://ip地址:443

①、创建个py脚本(推荐方式)
import http.server
import ssl
import os

# 配置参数
SHARE_DIR = "D:/data"          # 共享目录
PORT = 443
KEYFILE = "D:/ssl/server.key"  # 私钥路径
CERTFILE = "D:/ssl/server.crt" # 证书路径

# 自定义处理类(绑定目录)
class CustomHandler(http.server.SimpleHTTPRequestHandler):
  def __init__(self, *args, **kwargs):
      super().__init__(*args, directory=SHARE_DIR, **kwargs)

# 启动服务器(兼容Python 3.10+)
server = http.server.HTTPServer(('0.0.0.0', PORT), CustomHandler)
# 使用更安全的SSLContext替代wrap_socket(推荐)
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain(certfile=CERTFILE, keyfile=KEYFILE)
server.socket = context.wrap_socket(server.socket, server_side=True)

print(f"HTTPS共享已启动: https://0.0.0.0:{PORT}")
server.serve_forever()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

Python极速搭建Linux/Windows文件下载服务器:一行命令实现局域网文件共享 支持HTTPS_HTTPS

浏览器访问:https://ip地址:443

Python极速搭建Linux/Windows文件下载服务器:一行命令实现局域网文件共享 支持HTTPS_文件下载服务器_02

2)、Linux:

安装OpenSSL
#Debian类(如Ubuntu)
apt -y install openssl
#RedHat类(如CentOS或银河麒麟V10)
yum -y install openssl
  • 1.
  • 2.
  • 3.
  • 4.
生成证书
# 生成自签名证书
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem \
  -days 365 -nodes \
  -subj "/C=CN/ST=Beijing/L=Beijing/O=MyDev/CN=localhost"
  • 1.
  • 2.
  • 3.
  • 4.
①使用自定义 Python 脚本(推荐方式)

通过脚本集成 SSL 支持,步骤如下: 创建脚本文件(如 https_server.py):

import http.server
import ssl
import socketserver

# 配置参数
PORT = 443
CERT_FILE = 'cert.pem'  # 证书路径
KEY_FILE = 'key.pem'    # 私钥路径
BIND_ADDR = '0.0.0.0'   # 绑定所有接口

# 创建 HTTPS 服务器
handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer((BIND_ADDR, PORT), handler)

# 启用 SSL
httpd.socket = ssl.wrap_socket(
  httpd.socket,
  keyfile=KEY_FILE,
  certfile=CERT_FILE,
  server_side=True
)

print(f"Serving HTTPS on {BIND_ADDR}:{PORT}")
httpd.serve_forever()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

启动服务器

python3 https_server.py
  • 1.

浏览器访问:https://ip地址:443

②通过管道操作绕过参数限制(需 Python 3.7+):
# 启动 HTTPS 服务器
python3 -c "from http.server import HTTPServer, SimpleHTTPRequestHandler; import ssl; server = HTTPServer(('0.0.0.0', 443), SimpleHTTPRequestHandler); server.socket = ssl.wrap_socket(server.socket, keyfile='key.pem', certfile='cert.pem', server_side=True); print('HTTPS on :443'); server.serve_forever()"
  • 1.
  • 2.

浏览器访问:https://ip地址:443

2.后台运行 & 日志管理
# 后台运行并输出日志
nohup python -m http.server 8000 > server.log 2>&1 & 

#Windows使用 PowerShell 或 CMD 直接运行,无原生后台支持。
  • 1.
  • 2.
  • 3.
  • 4.

三、示例

# 局域网文件共享(手机/电脑互传)
python3 -m http.server 9080 -b 0.0.0.0 -d /data
# 一般来说HTTP就足够了,如果需要HTTPS访问,看上面“二、进阶用法”即可满足需求
  • 1.
  • 2.
  • 3.

http.server的核心价值在于 极简临时服务。掌握 --bind--directory、端口设置即可覆盖 90% 场景,进阶需求(HTTPS/CGI)按需启用,但务必严守安全边界

复杂需求请移步我的NGINX博客 https://blog.51cto.com/u_15971294/13831552