【问题标题】:Sending a file via http in Python with bottle framework使用瓶子框架在 Python 中通过 http 发送文件
【发布时间】:2015-12-16 14:37:19
【问题描述】:

如何使用 Bottle 框架在 Python 中发送文件作为 HTTP 请求的响应?

对于上传此代码适用于我:

@route('/upload', method='POST')
def do_upload():
    category   = request.forms.get('category')
    upload     = request.files.get('upload')
    name, ext = os.path.splitext(upload.raw_filename)
    if ext not in ('.png','.jpg','.jpeg'):
        return 'File extension not allowed.'

    save_path = category
    upload.raw_filename = "hoho" + ext
    upload.save(save_path) # appends upload.filename automatically
    return 'OK'

【问题讨论】:

    标签: python http download wsgi bottle


    【解决方案1】:

    只需调用Bottle's static_file function 并返回结果:

    @route('/static/<filename:path>')
    def send_static(filename):
        return static_file(filename, root='/path/to/static/files')
    

    当使用 url /static/myfile.txt 调用该示例时,将返回内容文件 /path/to/static/files/myfile.txt

    其他 SO 问题(如 this one)包含其他示例。

    【讨论】:

    • 我想知道是否可以使用bottle 提供静态文件,或者最好将nginx 放在它前面?
    猜你喜欢
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    • 2010-11-11
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    相关资源
    最近更新 更多