2

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

image

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.

Arda Nalbant
  • 479
  • 2
  • 7
  • 16

2 Answers2

2

The browser is requesting the favicon.

This is causing your code to fall over which is why you are seeing the 500 status code.

The formatting in the question is too far gone to look at, but the solution is to add a favicon. This answer here has more advice on the topic of favicons.

Community
  • 1
  • 1
Noelkd
  • 7,686
  • 2
  • 29
  • 43
  • First of all thanks for your attention and reply. I checked all comments on that link but im afraid nothing can help me out. Because i dont have and html code to disable favicon.ico request by: so i just want to delete or disable it via tornado or python code. hope you understand me thanks again – Arda Nalbant Mar 10 '16 at 12:20
  • In python you can't stop a browser making a request for a resource. The linked answer is just a pointer to other solutions. The key solution being fix the code because the browser is going to make the request. – Noelkd Mar 10 '16 at 13:48
0

Thats not what i want but following code dissapears my favicon.ico request I dont have favicon.ico therefore i want 404 error instead of 500 This trick comepletely remove the favicon request from "Chrome" Browser

if z == "ico":
        self.set_status(404)
Arda Nalbant
  • 479
  • 2
  • 7
  • 16
  • This doesn't remove the request for chrome it just returns a 404 Not Found – Noelkd Mar 10 '16 at 14:11
  • Oh understand you are right but when i hit f12 in chrome there is no favicon.ico anymore. Doesnt that mean chrome stops request for favicon.ico ? – Arda Nalbant Mar 10 '16 at 14:15
  • @ArdaNalbant No. Browser will still request for favicon. You can see the request in Chrome's Developer Tools. – xyres Mar 11 '16 at 22:55