最近,我使用web.py构建我的应用程序,发现每个请求都传输了我的静态文件。
以下是我的日志:
10.18.40.100:49458 - - [19/Jun/2014 17:03:45] "HTTP/1.1 GET /css/bootstrap.css" - 200 OK
10.18.40.100:49459 - - [19/Jun/2014 17:03:45] "HTTP/1.1 GET /css/bootstrap-responsive.css" - 200 OK
10.18.40.100:49460 - - [19/Jun/2014 17:03:45] "HTTP/1.1 GET /js/jquery-1.8.2.min.js" - 200 OK
10.18.40.100:49461 - - [19/Jun/2014 17:03:45] "HTTP/1.1 GET /js/bootstrap.js" - 200 OK
10.18.40.100:49461 - - [19/Jun/2014 17:03:45] "HTTP/1.1 GET /img/mini.png" - 200 OK
10.18.40.100:49460 - - [19/Jun/2014 17:03:45] "HTTP/1.1 GET /img/test_mini.png" - 200 OK
10.18.40.100:49460 - - [19/Jun/2014 17:03:45] "HTTP/1.1 GET /favicon.ico" - 200 OK
10.18.40.100:49460 - - [19/Jun/2014 17:03:52] "HTTP/1.1 GET /compeval" - 200 OK
10.18.40.100:49460 - - [19/Jun/2014 17:03:52] "HTTP/1.1 GET /css/bootstrap.css" - 200 OK
10.18.40.100:49458 - - [19/Jun/2014 17:03:52] "HTTP/1.1 GET /js/bootstrap.js" - 200 OK
10.18.40.100:49459 - - [19/Jun/2014 17:03:52] "HTTP/1.1 GET /js/jquery-1.8.2.min.js" - 200 OK
10.18.40.100:49461 - - [19/Jun/2014 17:03:52] "HTTP/1.1 GET /css/bootstrap-responsive.css" - 200 OK
10.18.40.100:49459 - - [19/Jun/2014 17:03:53] "HTTP/1.1 GET /img/mini.png" - 200 OK
10.18.40.100:49459 - - [19/Jun/2014 17:03:53] "HTTP/1.1 GET /img/glyphicons-halflings.png" - 200 OK
10.18.40.100:49459 - - [19/Jun/2014 17:03:53] "HTTP/1.1 GET /favicon.ico" - 200 OK
10.18.40.100:49459 - - [19/Jun/2014 17:03:58] "HTTP/1.1 GET /viewcompqlab" - 200 OK
10.18.40.100:49459 - - [19/Jun/2014 17:03:58] "HTTP/1.1 GET /css/bootstrap.css" - 200 OK
10.18.40.100:49461 - - [19/Jun/2014 17:03:58] "HTTP/1.1 GET /css/bootstrap-responsive.css" - 200 OK
10.18.40.100:49460 - - [19/Jun/2014 17:03:58] "HTTP/1.1 GET /js/bootstrap.js" - 200 OK
10.18.40.100:49458 - - [19/Jun/2014 17:03:58] "HTTP/1.1 GET /js/jquery-1.8.2.min.js" - 200 OK
10.18.40.100:49458 - - [19/Jun/2014 17:03:58] "HTTP/1.1 GET /img/mini.png" - 200 OK
10.18.40.100:49458 - - [19/Jun/2014 17:03:58] "HTTP/1.1 GET /favicon.ico" - 200 OK
10.18.40.100:49517 - - [19/Jun/2014 17:07:05] "HTTP/1.1 GET /compeval" - 200 OK
10.18.40.100:49517 - - [19/Jun/2014 17:07:05] "HTTP/1.1 GET /css/bootstrap.css" - 200 OK
10.18.40.100:49519 - - [19/Jun/2014 17:07:06] "HTTP/1.1 GET /js/jquery-1.8.2.min.js" - 200 OK
10.18.40.100:49518 - - [19/Jun/2014 17:07:06] "HTTP/1.1 GET /css/bootstrap-responsive.css" - 200 OK
10.18.40.100:49520 - - [19/Jun/2014 17:07:06] "HTTP/1.1 GET /js/bootstrap.js" - 200 OK
10.18.40.100:49520 - - [19/Jun/2014 17:07:06] "HTTP/1.1 GET /img/baidu_mini.png" - 200 OK
10.18.40.100:49518 - - [19/Jun/2014 17:07:06] "HTTP/1.1 GET /img/glyphicons-halflings.png" - 200 OK
10.18.40.100:49518 - - [19/Jun/2014 17:07:06] "HTTP/1.1 GET /favicon.ico" - 200 OK
显然,.png
,.ico
,.css
和.js
文件在我的服务器中是静态且全局有效的。所以,我的问题是:
如何避免这种情况并让浏览器缓存这些静态文件,除非按 Ctrl + F5 ?
附录,如何在web.py中提供静态文件,在我的index.py文件中提供如下代码:
"""register serve"""
urls=('/', 'index',
'/favicon.ico','favicon',
'/(js|css|img)/(.*)', 'static')
"""def class to support serve"""
class static:
def GET(self, media, fn):
rmod="r"
if fn.endswith(".png"):
rmod="rb"
f = open(media+'/'+fn, rmod)
try:
stream = f.read()
return stream
except:
f.close()
return '404 Not Found'
class favicon:
def GET(self):
f = open("static/favicon.ico", 'rb')
return f.read()
然后在我的模板html文件中使用这些静态文件服务器:
<link href="css/bootstrap.css" rel="stylesheet" media="screen">
<link href="css/bootstrap-responsive.css" rel="stylesheet">
<script src="js/jquery-1.8.2.min.js"></script>
<script src="js/bootstrap.js"></script>
答案 0 :(得分:0)
不应通过web.py
提供静态文件。相反,您应该配置您的Web服务器,以便在对相应URL的请求中为它们提供服务 - 常见的设置是提供static
目录,其中包含所有静态文件。
有关如何配置静态文件服务的详细信息将根据您的网络服务器(apache,lighttpd,...)和接口技术(fastCGI,mod_wsgi,...)而有所不同。您可以在the cookbook
的部署部分找到常见配置