【问题标题】:create a download command with web.py使用 web.py 创建下载命令
【发布时间】:2023-04-01 01:41:01
【问题描述】:

我是 web.py 的新手,我尝试创建一个下载命令。

我创建了这个:

import web

urls = (
    '/', 'index',
    '/download', 'Download'
)

class index:
    def GET(self):
        return "Hello, world!"

class Download:
    def GET(self):
           path = 'http:\\localhost:8080\C:\11\229077_6482396906_558_n.jpg' 
           web.header('Content-Disposition', 'attachment; filename="fname.ext"')
           web.header('Content-type','images/jpeg')
           web.header('Content-transfer-encoding','binary') 
           return open(path, 'rb').read()

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()

我有两个主要问题:

  1. 当我输入 http://localhost:8080/download 时,它给了我 500 内部服务器错误。为什么?

  2. 我无法选择要下载的文件(只需手动更改路径参数)。我如何给这个函数外部参数?

【问题讨论】:

  • 检查path 从您尝试读取文件的位置。它只是找不到那个文件。

标签: python web.py


【解决方案1】:
  1. 500 是通用错误代码,因此很难诊断。在设置您的代码时,我会在浏览器中检查您的 URI,以确定您的服务器是否正常运行。错误代码的完整列表 - link

  2. 您只能找到具有指向它们的链接的文件。例如,参见these wget commands

【讨论】:

    【解决方案2】:

    2.例如将其添加到您的代码中:

    filename = web.input().file
    

    所以它会在输入链接时返回所需的文件名:

    yoursite.tld/downloads?file=FILENAME
    

    在此处阅读有关此主题的更多信息:http://webpy.org/cookbook/input

    【讨论】:

      猜你喜欢
      • 2012-12-31
      • 1970-01-01
      • 2019-05-30
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多