大家好我刚刚创建了一个python程序,它按照格式打开我的文件并设置缓存控制时间。但问题是我的代码采用格式为ico,当我请求jpg文件时来自favicon
这是我的代码:
#!/usr/bin/python
import tornado
import tornado.ioloop
import tornado.web
import sys , importlib
import os
import os.path
from urllib2 import Request, urlopen, URLError
from myconfig import *
import webbrowser
import time
conf2=locals()
if len(sys.argv) > 1:
conf = sys.argv[1]
else:
conf = 'dosya belirtilmedi'
print conf
def check( str ):
return conf2[str][0]
def check2( str ):
return conf2[str][1]
def check3( str ):
return conf2[str][2]
def do( str ):
return len(str)
def calculate(str):
if check3(str) == 'seconds':
return 1
elif check3(str) == 'minutes':
return 60
elif check3(str) == 'hours':
return 60*60
elif check3(str) == 'days':
return 60*60*24
elif check3(str) == 'weeks':
return 60*60*24*7
elif check3(str) == 'mounths':
return 60*60*24*7*4
elif check3(str) == 'years':
return 60*60*24*7*4*12
else:
return 5000
class GetFileHandler(tornado.web.RequestHandler):
def get(self,url):
x={}
f={}
path = sys.argv[1] + self.request.uri
data = self.request.uri
x = data.split(".")
if data == '/':
self.write("Ana Sayfa")
elif len(x) > 1:
z=x[1]
f=check(z).split("/")
j=f[1]
print j
print do(j)
print do(html)
if z in conf2:
self.set_header('Content-Type',check(z))
if (z in conf2 and do(j) != 3):
cal=calculate(z)
cal2=conf2[z][1]
total=cal*cal2
self.set_header('Cache-Control', 'max-age=%s, public' % total )
else:
self.set_header('Cache-Control', 'max-age=100, public')
print 100
if os.path.isfile(path) and os.access(path, os.R_OK):
with open(path, 'rb') as f:
y = f.read()
self.write(y)
self.finish()
else:
webbrowser.open('http://ecommerceblog.mightymerchant.com/wp-content/uploads/2007/10/404-image.jpg')
else:
self.write("YOK" + "=" + z)
else:
self.write("Dosya Formati Yok")
if __name__ == "__main__":
application = tornado.web.Application([
(r"/(.*)", GetFileHandler),])
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
这是我的配置文件:
jpg=["image/jpeg",23,"minutes"]
html=["text/html",30,"seconds"]
gif=["image/gif",3,"hours"]
png=["image/png"]
这是我的错误:
ERROR:tornado.application:Uncaught exception GET /favicon.ico (127.0.0.1) HTTPServerRequest(protocol='http', host='localhost:8888', method='GET', uri='/favicon.ico', version='HTTP/1.1', remote_ip='127.0.0.1', headers={'Accept-Language': 'en-US,en;q=0.8', 'Accept-Encoding': 'gzip, deflate, sdch', 'Host': 'localhost:8888', 'Accept': '*/*', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36', 'Connection': 'keep-alive', 'Referer': 'http://localhost:8888/im.jpg'}) Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/tornado-4.3-py2.7-linux-x86_64.egg/tornado/web.py", line 1443, in _execute result = method(*self.path_args, **self.path_kwargs) File "./w.py", line 60, in get f=check(z).split("/") File "./w.py", line 22, in check return conf2[str][0] KeyError: 'ico' ERROR:tornado.access:500 GET /favicon.ico (127.0.0.1) 0.48ms
以下是我在浏览器上的问题图片。正如你所看到我只发送jpg请求但总是有favicon请求(内容类型是text / html)。我只是想禁用它可能会删除它。我无法理解原因。请帮助我。