我正在建立一个私人文件上传网站.爱丽丝上传文件,鲍勃下载文件.
除了Alice和Bob之外的其他人不应具有访问权限.我最初是在考虑为文件提供一个复杂的名称(http://domain/download/md5sum.zip),但是我想要一个有效的链接.因此,例如http://domain/download/tempkey/aaa123/file.zip.这将使我对文件下载和日志记录有更多控制.
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
# The URL the client requested
print self.path
# analyze self.path, map the local file location...
# open the file, load the data
with open('test.py') as f: data = f.read()
# send the headers
self.send_response(200)
self.send_header('Content-type', 'application/octet-stream') # you may change the content type
self.end_headers()
# If the file is not found, send error code 404 instead of 200 and display a message accordingly, as you wish.
# wfile is a file-like object. writing data to it will send it to the client
self.wfile.write(data)
但是,如何在Django中使用它呢?视图函数应该返回一个HTTPResponse对象,但不会这样做.