對比傳統方案:
| 方案 | 部署複雜度 | 依賴 | 適用場景 |
|---|---|---|---|
| FTP (vsftpd) | 高 (配置文件、用戶、權限、防火牆) | 額外安裝 | 生產環境長期服務 |
| NFS/Samba | 高 (內核模塊、導出配置、掛載) | 套件安裝 | 持久化網絡存儲 |
| SimpleHTTPServer | 極低 (單命令) | Python 內建 | 臨時、快速、內網分享 |
index.html 則作為首頁,否則 自動列出目錄結構# 1. 進入待分享目錄
cd /home/abc/project_files
# 2. 啟動 (默認 8000 端口)
python -m SimpleHTTPServer
# 3. 或指定端口
python -m SimpleHTTPServer 8080
# 輸出示例
Serving HTTP on 0.0.0.0 port 8000 ...
192.168.20.33 - - [09/Jan/2016 15:13:28] "GET / HTTP/1.1" 200 -
192.168.20.33 - - [09/Jan/2016 15:13:54] "GET /jdk-7u79-linux-x64.tar.gz HTTP/1.1" 200 -
# 模塊合併為 http.server
python3 -m http.server 8000
# 綁定特定 IP (僅本機訪問)
python3 -m http.server 8000 --bind 127.0.0.1
# 指定服務目錄 (不進入目錄)
python3 -m http.server 8000 --directory /path/to/share
https://:8000/ wget https://:8000/file.tar.gz requests.get()、curl 等# Ubuntu/Debian (ufw)
sudo ufw allow 8000/tcp
# CentOS/RHEL (firewalld)
sudo firewall-cmd --add-port=8000/tcp --permanent
sudo firewall-cmd --reload
# 通用 iptables
sudo iptables -A INPUT -p tcp --dport 8000 -j ACCEPT
# 使用完畢關閉
sudo ufw delete allow 8000/tcp
# 後台運行,日誌重定向
nohup python3 -m http.server 8000 > server.log 2>&1 &
# 查看進程
ps aux | grep http.server
# 停止
kill $(pgrep -f "http.server 8000")
# 建立 .htaccess 風格密碼文件 (需安裝 http.server 非標準擴展)
# 更推薦:使用 nginx/Apache 反向代理添加 auth_basic
# 生成自簽證書
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes
# Python 3 啟動 HTTPS
python3 -c "
import http.server, ssl
server = http.server.HTTPServer(('0.0.0.0', 4443), http.server.SimpleHTTPRequestHandler)
server.socket = ssl.wrap_socket(server.socket, keyfile='key.pem', certfile='cert.pem', server_side=True)
server.serve_forever()
"
# 安裝 Python (勾選 Add to PATH)
# 打開 CMD/PowerShell 進入目錄
python -m http.server 8000
# 防火牆:Windows Defender 防火牆 → 允許應用 → Python → 勾選專用/公用網絡
| 工具 | 命令 | 特點 |
|---|---|---|
| Node.js http-server | npx http-server -p 8000 | 需 Node 環境、功能豐富 (緩存、壓縮、CORS) |
| PHP 內建服務器 | php -S 0.0.0.0:8000 | 支援 PHP 腳本執行 |
| Ruby WEBrick | ruby -run -e httpd . -p 8000 | Ruby 環境 |
| Go 靜態服務器 | go run serve.go | 單二進制、高效能 |
| busybox httpd | httpd -f -p 8000 -h . | 嵌入式/容器極簡 |
「SimpleHTTPServer 是開發者工具箱中的瑞士軍刀——不完美,但關鍵時刻最好用。」