【问题标题】:Redirect URL to Text File in Python在 Python 中将 URL 重定向到文本文件
【发布时间】:2020-02-24 14:47:15
【问题描述】:

我正在尝试将 pastebin.com/raw/example.txt 之类的 url 重新路由到我电脑上的本地文本文件。任何人都可以帮助我使用:

from flask import Flask
from flask import send_file
app = Flask(__name__)

@app.route("/raw/example.txt")
def request_example():
    data = "Example"
    return data

app.run(host='0.0.0.0', port=80)

那么我需要在 host='' 中添加什么才能使其工作?或改变路线定义?非常感谢

【问题讨论】:

  • 你可以试试这个。 @app.route("/raw/example") def request_example(): try: return send_file('/path/to/local/file.txt', attachment_filename='example.txt') except Exception as e: return str(e)

标签: python python-3.x flask python-requests


【解决方案1】:

看看这个:

from flask import Flask
from flask import send_file
app = Flask(__name__)

@app.route('/raw/example')
def return_files_tut():
    try:
        return send_file('/path/to/local/file.txt', attachment_filename='example.txt')
    except Exception as e:
        return str(e)

【讨论】:

  • 这很有用,但不是我想要的。我想知道如何使用 app.run 函数,因为我应该在主机和端口中放置什么以获取 https 链接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2017-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多