我正在为一个小项目使用 web.py,并且我希望用户能够在服务器上的/files 目录中访问文件。我似乎无法找到如何根据 GET 请求返回文件,所以我不知道该怎么做。
本质上到底想做的是:
urls = ('/files/+', 'files')
class files:
def GET(self)
#RETURN SOME FILE
有没有简单的方法从 GET 请求返回文件?
请您参考如下方法:
我想出了这个 webpy GET 方法:
def GET(self):
request = web.input( path=None )
getPath = request.path
if os.path.exists( getPath ):
getFile = file( getPath, 'rb' )
web.header('Content-type','application/octet-stream')
web.header('Content-transfer-encoding','base64')
return base64.standard_b64encode( getFile.read( ) )
else:
raise web.notfound( )
当其他受访者建议您仔细考虑安全隐患时,他们是正确的。在我的例子中,我们将把这样的代码包含到管理 Web 服务中,该服务将(应该!)仅在我们的内部 LAN 中可用。
