python - Flask系统找不到指定的路径: '130.127.5.9'

标签 python flask

我刚刚开始尝试使用 flask 我正在尝试列出使用它的网络驱动程序。这是我在浏览器上输入的命令。但我得到一个错误,它找不到路径 http://127.0.0.1:5000/130.13.5.8/D/dir/

该功能适用​​于本地驱动程序,没有问题

我知道为什么失败了。它需要在实际 ip 之前有 2 个 '\' 或 4 个 '\\' 。 但是当我尝试 http://127.0.0.1:5000/////130.13.5.8/D/dir/ 它不起作用。 我什至尝试过 %F%F 它似乎也不起作用。

@app.route('/<path:filepath>/dir/')
def get_dir(filepath):
    dir_listing = ''
    for entry in os.listdir(filepath):
        entry_type = 'dir' if os.path.isdir(os.path.join(filepath, entry)) else 'file'
        dir_listing += '{entry_name}|{entry_type}|'.format(entry_name=entry, entry_type=entry_type)
    return dir_listing

最佳答案

对于遇到同样问题的人,我的解决方法如下

   from ipaddress import ip_address
    try:
        ip_address(filepath.split('/')[0])
        filepath = '\\\\{filepath}'.format(filepath=filepath)
    except ValueError as e:
        pass

关于python - Flask系统找不到指定的路径: '130.127.5.9' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57223875/

相关文章:

fmtp 的 Python 正则表达式

python - 如何根据 Python 中的条件停止播放音频?

java - 如何从 Python Struct.pack 读取 Java 数据

python - flask:跨 View 函数传递参数

python - 未注册的多页 Dash 应用回调

python - ctypes 来自其他线程的异步回调

python - 无法从 gi.repository 导入模块

javascript - Flask ajax 请求后不要重新加载网页

html - 在 wtforms SubmitField 中使用图标?

python - 如何覆盖 Flask-Security 默认消息?