Python 从本地文件夹下载文件

Python 从本地文件夹下载文件,python,flask,Python,Flask,我正在尝试这个代码 @app.route('/process/<user_id>/<file_format>/<download>', methods=['POST', 'GET']) def download(user_id, file_format, download): if request.method == 'GET': response = urllib2.urlopen("http://"+socket.gethostn

我正在尝试这个代码

@app.route('/process/<user_id>/<file_format>/<download>', methods=['POST', 'GET'])
def download(user_id, file_format, download):
    if request.method == 'GET':

        response = urllib2.urlopen("http://"+socket.gethostname()+"app/documents/"+ download)
        html = response.read()
        return html

那么,基本上我怎样才能从我的文档文件夹中获取文件呢?

从你的代码中我知道你想下载一个位于你自己机器上的html文件。 第一:

应该是

response = urllib2.urlopen("http://"+socket.gethostname()+"/app/documents/"+ download)
除非“app”是主机名的一部分(我认为不是)

第二。。。确保您可以使用wget执行相同的操作(这意味着您在本地安装了一个http服务器,该服务器侦听端口80并可以为请求的文件提供服务):

wget
从烧瓶导入烧瓶,重定向
app=烧瓶(名称)
@app.route('/process//',methods=['POST','GET'])
def下载(用户id、文件格式、下载):
如果request.method==“GET”:
返回重定向(“http://{0}/app/documents/{1}”。格式(request.host,下载))
@app.route('/process//'))
def下载(用户id、文件格式、下载):
返回重定向(url_表示('static',filename='documents/'+下载))

这就是我要找的。

。。。socket.gethostname()+“/app…?@sunn0,现在找不到我了。文件保存在这里path=“app/documents/”+doc\u name,其中doc\u name等于doc\u name=“thereport\u”+str(rand)+”。“+找不到formatoutput http://127.0.0.1:5000/app/documents/thereport_003e4242-9cff-410b-8258-db1cfa9fb7c8.pdf这是另一个问题。你的文件不在那里,主要是因为你需要把它放在静态目录中。如果我没有你的路由(应用程序/文档),显然我会得到404。
ValueError: unknown url type: app/documents/thereport_1712818a-39a3-436e-985c-84f1e8d43346.pdf
response = urllib2.urlopen("http://"+socket.gethostname()+"app/documents/"+ download)
response = urllib2.urlopen("http://"+socket.gethostname()+"/app/documents/"+ download)
wget <URL>
from flask import Flask, redirect
app = Flask(__name__)

@app.route('/process/<user_id>/<file_format>/<download>', methods=['POST', 'GET'])
def download(user_id, file_format, download):
    if request.method == 'GET':
        return redirect("http://{0}/app/documents/{1}".format(request.host,download))
@app.route('/process/<user_id>/<file_format>/<download>')
def download(user_id, file_format, download):
    return redirect(url_for('static', filename='documents/'+download))