簡體   English   中英

如何在webpy中提供文件?

[英]How to serve file in webpy?

我正在使用webpy framefork。 我想在其中一個請求上提供靜態文件。 在webpy框架中是否有特殊方法或者我只需要讀取並返回該文件?

如果您正在運行開發服務器(沒有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()

在過去的幾個小時里我一直在努力...哎呀!

找到兩個對我有用的解決方案...... 1 - 在.htaccess中在ModRewrite行之前添加這一行:

RewriteCond %{REQUEST_URI} !^/static/.*

這將確保不會重寫對/ 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

注意 - 我從web.py google群組中偷了這個,但是找不到dang post了!

這些都適用於我,在web.py的模板中以及直接調用我放入“靜態”的網頁

我不建議使用web.py提供靜態文件。 你最好配置apache或nginx。

其他答案對我不起作用。 您可以先在app.py中加載html文件,或者甚至在app.py中編寫html。 然后,您可以使索引類'GET方法返回靜態html。

index_html = '''<html>hello world!</html>'''

class index:
    def GET(self):
        return index_html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM