‘http://localhost:5001/static/picture/11.png’ 我只要’static/picture/11.png‘ 用python怎么写
时间: 2023-07-12 16:01:00 浏览: 47
你可以使用 Python 的字符串切片(slice)来获取相对路径(即去掉域名和端口号的路径)。具体来说,可以使用以下代码:
```python
url = 'http://localhost:5001/static/picture/11.png'
relative_path = url.split('static/')[1]
print(relative_path) # 输出:picture/11.png
```
在这个例子中,我们首先使用 `split` 函数将 URL 字符串分割成两部分,即域名和相对路径。然后,我们从相对路径中取出了 `static/` 后面的部分,得到了最终的相对路径。你可以将 `relative_path` 变量放入 `send_file` 函数中,如下所示:
```python
from flask import Flask, send_file
app = Flask(__name__)
@app.route('/picture')
def get_picture():
url = 'http://localhost:5001/static/picture/11.png'
relative_path = url.split('static/')[1]
return send_file(relative_path, mimetype='image/png')
if __name__ == '__main__':
app.run(debug=True)
```
在这个例子中,我们假设图片文件的后缀名为 PNG,因此 MIME 类型为 `'image/png'`。