如何在webpy中提供文件?

cod*_*dez 8 python web.py

我正在使用webpy framefork.我想在其中一个请求上提供静态文件.在webpy框架中是否有特殊方法或者我只需要读取并返回该文件?

mik*_*iku 11

如果您正在运行开发服务器(没有apache):

在运行web.py服务器的脚本位置创建名为static的目录(也称为文件夹).然后将要提供的静态文件放在静态文件夹中.

例如,URL http://localhost/static/logo.png会将图像./static/logo.png发送到客户端.

参考:http://webpy.org/cookbook/staticfiles


更新.如果您确实需要提供静态文件,/只需使用重定向:

#!/usr/bin/env python

import web

urls = (
  '/', 'index'
)

class index:
    def GET(self):
        # redirect to the static file ...
        raise web.seeother('/static/index.html')

app = web.application(urls, globals())

if __name__ == "__main__": app.run()
Run Code Online (Sandbox Code Playgroud)


tom*_*ton 6

在过去的几个小时里我一直在努力...哎呀!

找到两个对我有用的解决方案...... 1 - 在.htaccess中在ModRewrite行之前添加这一行:

RewriteCond %{REQUEST_URI} !^/static/.*
Run Code Online (Sandbox Code Playgroud)

这将确保不会重写对/ static /目录的请求以转到您的code.py脚本.

2 - 在code.py中为每个目录添加一个静态处理程序和一个url条目:

urls = (
    '/' , 'index' ,
    '/add', 'add' ,
    '/(js|css|images)/(.*)', 'static', 
    '/one' , 'one'
    )

class static:
    def GET(self, media, file):
        try:
            f = open(media+'/'+file, 'r')
            return f.read()
        except:
            return '' # you can send an 404 error here if you want
Run Code Online (Sandbox Code Playgroud)

注意 - 我从web.py google群组中偷了这个,但是找不到dang post了!

这些都适用于我,在web.py的模板中以及直接调用我放入"静态"的网页