Hi everyone i just made a python program that opens my files by their formats and set their cache control times. But the problem is my my code takes format as ico which comes from favicon when i request jpg file
This is my code :
#!/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()
This is my configure file:
jpg=["image/jpeg",23,"minutes"]
html=["text/html",30,"seconds"]
gif=["image/gif",3,"hours"]
png=["image/png"]
This is my Error:
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
Here is a image of my problem on browser. As you can see i only send jpg request but always there is favicon request(which content type is text/html).I just want to disable it maybe delete it. i can't understand the reason. Please help me thanks.