Python 静态文件子文件夹中的视频不工作
我发现了这段代码:Python 静态文件子文件夹中的视频不工作,python,flask,mp4,Python,Flask,Mp4,我发现了这段代码: from flask import Flask import flask from os import listdir import os.path import pathlib import ntpath app = Flask(__name__) @app.route('/', defaults={'req_path': ''}) @app.route('/<path:req_path>') def dir_listing(req_path): B
from flask import Flask
import flask
from os import listdir
import os.path
import pathlib
import ntpath
app = Flask(__name__)
@app.route('/', defaults={'req_path': ''})
@app.route('/<path:req_path>')
def dir_listing(req_path):
BASE_DIR = 'D:/Users/xxx/projects/project1/static'
# Joining the base and the requested path
abs_path = os.path.join(BASE_DIR, req_path)
# Return 404 if path doesn't exist
if not os.path.exists(abs_path):
return flask.abort(404)
# Check if path is a file and serve
if os.path.isfile(abs_path):
return flask.send_file(abs_path)
# Show directory contents
files = os.listdir(abs_path)
for i, f in enumerate(files):
files[i] = os.path.join(req_path, f).replace('\\', '/')
return flask.render_template('files.html', files=files)
if __name__ == '__main__':
app.run(host='10.13.0.33', port=80)
当我访问网页时,我会看到:
当我单击FirstFolder链接时,我得到以下信息:
当我单击第二个文件夹链接时,我得到以下信息:
我让它工作了:
app.py:
import flask
import ntpath
import os
import socket
host_name = socket.gethostname()
if host_name == 'hostname1':
static_folder = '/some/path/to/videos'
ip_address = '192.xxx.xxx.xxx'
port = 80
elif host_name == "hostname2":
static_folder = '/another/path/to/videos'
ip_address = '10.xxx.xxx.xxx'
port = 8000
else:
raise Exception('hostname is not defined!')
def remove_static_folder(path):
path_to_file = path.replace(static_folder, '/')
return path_to_file
def get_folders(folder):
if folder is not None and os.path.isdir(folder):
folders = []
for f in os.listdir(folder):
path_to_folder = os.path.join(folder, f).replace('\\', '/')
if os.path.isdir(path_to_folder) and not f.startswith('.'):
folders.append([remove_static_folder(path_to_folder), ntpath.basename(path_to_folder)])
return folders
return []
def get_videos(folder):
if os.path.isdir(folder):
print("is dir")
if folder is not None and os.path.isdir(folder):
videos = []
for f in os.listdir(folder):
if os.path.isfile(os.path.join(folder, f)) and not f.startswith('.') and f.endswith('.mp4'):
path_to_video = os.path.join(folder, f).replace('\\', '/')
videos.append([remove_static_folder(path_to_video), ntpath.basename(f)])
return videos
return []
app = flask.Flask(__name__, static_folder=static_folder,
static_url_path= os.path.join(os.path.sep, os.path.basename(os.path.normpath(static_folder))))
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
if path is None or path == "":
folder = static_folder
else:
folder = os.path.join(static_folder, path).replace('\\', '/')
videos = get_videos(folder)
folders = get_folders(folder)
folder = remove_static_folder(folder)
if folder.startswith(".") or folder.startswith('/'):
folder = folder[1:]
return flask.render_template("index.html", folder=folder, videos=videos, folders=folders)
if __name__ == '__main__':
if ip_address is None:
print("hostname cannot be determined")
exit(-1)
app.run(host=ip_address, port=port)
导入烧瓶
导入ntpath
导入操作系统
导入套接字
host_name=socket.gethostname()
如果主机名='hostname1':
静态文件夹=“/some/path/to/videos”
ip_地址='192.xxx.xxx.xxx'
端口=80
elif主机名==“主机名2”:
静态文件夹='/other/path/to/videos'
ip_地址='10.xxx.xxx.xxx'
端口=8000
其他:
引发异常('未定义主机名!')
def删除静态文件夹(路径):
path_to_file=path.replace(静态_文件夹“/”)
将路径\u返回到\u文件
def get_文件夹(文件夹):
如果文件夹不是None和os.path.isdir(文件夹):
文件夹=[]
对于os.listdir(文件夹)中的f:
path\u to\u folder=os.path.join(folder,f).replace(“\\”,“/”)
如果os.path.isdir(路径到文件夹)而不是f.startswith('.'):
folders.append([删除\u静态\u文件夹(路径到\u文件夹),ntpath.basename(路径到\u文件夹)])
返回文件夹
返回[]
def get_视频(文件夹):
如果os.path.isdir(文件夹):
打印(“is dir”)
如果文件夹不是None和os.path.isdir(文件夹):
视频=[]
对于os.listdir(文件夹)中的f:
如果os.path.isfile(os.path.join(folder,f))而不是f.startswith('.')和f.endswith('.mp4'):
path\u to\u video=os.path.join(文件夹,f).replace(“\\”,“/”)
videos.append([删除\u静态\u文件夹(路径到\u视频),ntpath.basename(f)])
返回视频
返回[]
app=flask.flask(\uuuuu name\uuuuuuuuuuu,静态文件夹=静态文件夹,
静态路径=os.path.join(os.path.sep,os.path.basename(os.path.normpath(静态文件夹)))
@app.route('/',默认值={'path':''})
@应用程序路径(“/”)
def catch_all(路径):
如果路径为无或路径=“”:
文件夹=静态文件夹
其他:
folder=os.path.join(静态\u文件夹,路径)。替换(“\\”,“/”)
视频=获取视频(文件夹)
文件夹=获取文件夹(文件夹)
文件夹=删除\u静态\u文件夹(文件夹)
如果folder.startswith(“.”)或folder.startswith(“/”):
文件夹=文件夹[1:]
返回flask.render_模板(“index.html”,文件夹=文件夹,视频=视频,文件夹=文件夹)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
如果ip_地址为无:
打印(“无法确定主机名”)
出口(-1)
应用程序运行(主机=ip地址,端口=端口)
index.html:
<ul>
{% for file in files %}
<li><a href="{{ file }}">{{ file }}</a></li>
{% endfor %}
</ul>
<!DOCTYPE html>
<html>
<head>
<title>{{ folder }}</title>
</head>
<body>
<div>
<h1>{{ folder}} </h1>
<ul>
{% for video in videos %}
<div class="container">
<video width="640" controls preload="metadata">
<source src="{{ url_for('static', filename=video[0]) }}" type="video/mp4">
</video>
<div class="top">{{ video[1] }}</div>
</div>
{% endfor %}
</ul>
<ul>
{% for folder in folders %}
<a href="{{ folder[0] }}">{{ folder[1] }} <br>
{% endfor %}
</ul>
</div>
</body>
</html>
{{文件夹}}
{{文件夹}}
{%用于视频中的视频%}
{{视频[1]}
{%endfor%}
{文件夹%中的文件夹为%s}
{{文件夹[1]}}
{%endfor%}
我让它工作了:
app.py:
import flask
import ntpath
import os
import socket
host_name = socket.gethostname()
if host_name == 'hostname1':
static_folder = '/some/path/to/videos'
ip_address = '192.xxx.xxx.xxx'
port = 80
elif host_name == "hostname2":
static_folder = '/another/path/to/videos'
ip_address = '10.xxx.xxx.xxx'
port = 8000
else:
raise Exception('hostname is not defined!')
def remove_static_folder(path):
path_to_file = path.replace(static_folder, '/')
return path_to_file
def get_folders(folder):
if folder is not None and os.path.isdir(folder):
folders = []
for f in os.listdir(folder):
path_to_folder = os.path.join(folder, f).replace('\\', '/')
if os.path.isdir(path_to_folder) and not f.startswith('.'):
folders.append([remove_static_folder(path_to_folder), ntpath.basename(path_to_folder)])
return folders
return []
def get_videos(folder):
if os.path.isdir(folder):
print("is dir")
if folder is not None and os.path.isdir(folder):
videos = []
for f in os.listdir(folder):
if os.path.isfile(os.path.join(folder, f)) and not f.startswith('.') and f.endswith('.mp4'):
path_to_video = os.path.join(folder, f).replace('\\', '/')
videos.append([remove_static_folder(path_to_video), ntpath.basename(f)])
return videos
return []
app = flask.Flask(__name__, static_folder=static_folder,
static_url_path= os.path.join(os.path.sep, os.path.basename(os.path.normpath(static_folder))))
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
if path is None or path == "":
folder = static_folder
else:
folder = os.path.join(static_folder, path).replace('\\', '/')
videos = get_videos(folder)
folders = get_folders(folder)
folder = remove_static_folder(folder)
if folder.startswith(".") or folder.startswith('/'):
folder = folder[1:]
return flask.render_template("index.html", folder=folder, videos=videos, folders=folders)
if __name__ == '__main__':
if ip_address is None:
print("hostname cannot be determined")
exit(-1)
app.run(host=ip_address, port=port)
导入烧瓶
导入ntpath
导入操作系统
导入套接字
host_name=socket.gethostname()
如果主机名='hostname1':
静态文件夹=“/some/path/to/videos”
ip_地址='192.xxx.xxx.xxx'
端口=80
elif主机名==“主机名2”:
静态文件夹='/other/path/to/videos'
ip_地址='10.xxx.xxx.xxx'
端口=8000
其他:
引发异常('未定义主机名!')
def删除静态文件夹(路径):
path_to_file=path.replace(静态_文件夹“/”)
将路径\u返回到\u文件
def get_文件夹(文件夹):
如果文件夹不是None和os.path.isdir(文件夹):
文件夹=[]
对于os.listdir(文件夹)中的f:
path\u to\u folder=os.path.join(folder,f).replace(“\\”,“/”)
如果os.path.isdir(路径到文件夹)而不是f.startswith('.'):
folders.append([删除\u静态\u文件夹(路径到\u文件夹),ntpath.basename(路径到\u文件夹)])
返回文件夹
返回[]
def get_视频(文件夹):
如果os.path.isdir(文件夹):
打印(“is dir”)
如果文件夹不是None和os.path.isdir(文件夹):
视频=[]
对于os.listdir(文件夹)中的f:
如果os.path.isfile(os.path.join(folder,f))而不是f.startswith('.')和f.endswith('.mp4'):
path\u to\u video=os.path.join(文件夹,f).replace(“\\”,“/”)
videos.append([删除\u静态\u文件夹(路径到\u视频),ntpath.basename(f)])
返回视频
返回[]
app=flask.flask(\uuuuu name\uuuuuuuuuuu,静态文件夹=静态文件夹,
静态路径=os.path.join(os.path.sep,os.path.basename(os.path.normpath(静态文件夹)))
@app.route('/',默认值={'path':''})
@应用程序路径(“/”)
def catch_all(路径):
如果路径为无或路径=“”:
文件夹=静态文件夹
其他:
folder=os.path.join(静态\u文件夹,路径)。替换(“\\”,“/”)
视频=获取视频(文件夹)
文件夹=获取文件夹(文件夹)
文件夹=删除\u静态\u文件夹(文件夹)
如果folder.startswith(“.”)或folder.startswith(“/”):
文件夹=文件夹[1:]
返回flask.render_模板(“index.html”,文件夹=文件夹,视频=视频,文件夹=文件夹)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
如果ip_地址为无:
打印(“无法确定主机名”)
出口(-1)
应用程序运行(主机=ip地址,端口=端口)
index.html:
<ul>
{% for file in files %}
<li><a href="{{ file }}">{{ file }}</a></li>
{% endfor %}
</ul>
<!DOCTYPE html>
<html>
<head>
<title>{{ folder }}</title>
</head>
<body>
<div>
<h1>{{ folder}} </h1>
<ul>
{% for video in videos %}
<div class="container">
<video width="640" controls preload="metadata">
<source src="{{ url_for('static', filename=video[0]) }}" type="video/mp4">
</video>
<div class="top">{{ video[1] }}</div>
</div>
{% endfor %}
</ul>
<ul>
{% for folder in folders %}
<a href="{{ folder[0] }}">{{ folder[1] }} <br>
{% endfor %}
</ul>
</div>
</body>
</html>
{{文件夹}}
{{文件夹}}
{%用于视频中的视频%}
{{视频[1]}
{%endfor%}
{文件夹%中的文件夹为%s}
{{文件夹[1]}}
{%endfor%}
我对你的具体问题没有答案,但看起来你在“重新发明轮子”。Flask为您的html提供了方便的使用方法。我感谢您的帮助(如果我遗漏了什么,我真诚地道歉),但我不是想下载t