最简单的临时处理方式是在当前目录下python -m http.server


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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from flask import Flask, send_from_directory
import os

app = Flask(__name__)

root_dir = r'C:/Users/tellw'

def root_dir_file_list():
file_list_str = ''
for filename in os.listdir(root_dir):
if os.path.isdir(root_dir+'/'+filename):
li = f'''
<li><a href="/{filename}/">{filename}/</a></li>'''
else:
li = f'''
<li><a href="/{filename}">{filename}</a></li>'''
file_list_str = file_list_str + li
return file_list_str

def sub1_dir_file_list(folder):
if folder[-1] == '/':
folder = folder[:-1]
print(folder)
file_list_str = ''
for filename in os.listdir(os.path.join(root_dir,folder)):
if os.path.isdir(os.path.join(folder,filename)):
tail='/'
else:
tail=''
li = f'''
<li><a href="/{folder}/{filename}{tail}">{filename}{tail}</a></li>'''
file_list_str = file_list_str + li
return file_list_str

def create_html(file_list_str):
html = f'''
<html>
<head>
<title>Download</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>
</head>
<body>
<h1>Directory listing</h1>
<hr>
<ul>
{file_list_str}
</ul>
</body>
</html>
'''
return html


@app.route('/')
def index():
return create_html(root_dir_file_list())


@app.route('/<path:sub_dir>')
def sub_dir1_page(sub_dir):
# dir_name = root_dir + '/' + sub_dir
return create_html(sub1_dir_file_list(sub_dir))

@app.route('/<filename>')
def download_root(filename):
return send_from_directory(root_dir, filename)

@app.route('/<path:sub_dir>/<filename>')
def download_subdir1(sub_dir, filename):
dir_name = root_dir + '/' + sub_dir
return send_from_directory(dir_name, filename)

app.run(host='0.0.0.0',debug=True)
用url是否以'/'结尾区分“目录+目录”和“目录+文件”的路由,且以'/'结尾的链接文字表示要访问的是个目录,没有'/'结尾的链接文字表示要下载那个文件,无论目录有多少层,都能访问,path变量把所有的路径字符串全部接收,然后处理文件系统。

代码的思路来自flask学习笔记(三):文件浏览器+下载指定文件夹中的文件(包含子文件夹)


使用nginx配置文件下载站点,且没有层级限制

nginx配置文件的http块里添加如下代码

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
server {
listen 8085;
server_name localhost;
autoindex on;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root C:/Users/tellw;
# index index.html index.htm index.php;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root C:/Users/tellw/blog;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
location ~ \.php$ {
root C:/Users/tellw/blog;
#proxy_pass http://127.0.0.1;
# php 处理程序监听的tcp端口
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# 确定脚本名称
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# 传递请求参数
fastcgi_param QUERY_STRING $query_string;
# 必须要包含fastcgi_params预定义参数
include fastcgi_params;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

随后进入nginx目录,nginx -s reload重启nginx服务,访问http://localhost:8085,即可看到C:/Users/tellw目录下的文件浏览站点。

参考链接:通过nginx进行文件共享

创建于2023.3.23/18.36,修改于2023.6.7/17.33