lilzz
Posts: 411
Joined: Sat Nov 30, 2013 5:27 pm

What type of files can this web server serve?

Sun Mar 09, 2014 3:14 am

Code: Select all

def serveFile(self, relativePath):
        if self.server.docroot != None:
            path = self.findFile(self.server.docroot + "/" + relativePath)
            if path == None:
                path = self.findFile("./" + relativePath)

        else:
            path = self.findFile("./" + relativePath)                
            if path == None:
                path = self.findFile(WEBIOPI_DOCROOT + "/" + relativePath)

        if path == None and (relativePath.startswith("webiopi.") or relativePath.startswith("jquery")):
            path = self.findFile(WEBIOPI_DOCROOT + "/" + relativePath)

        if path == None:
            return self.sendResponse(404, "Not Found")

        realPath = os.path.realpath(path)

        if realPath.endswith(".py"):
            return self.sendResponse(403, "Not Authorized")

        if not (realPath.startswith(os.getcwd()) 
                or (self.server.docroot and realPath.startswith(self.server.docroot))
                or realPath.startswith(WEBIOPI_DOCROOT)):
            return self.sendResponse(403, "Not Authorized")

        (type, encoding) = mime.guess_type(path)
        f = codecs.open(path, encoding=encoding)
        data = f.read()
        f.close()
        self.send_response(200)
        self.send_header("Content-Type", type);
        self.send_header("Content-Length", os.path.getsize(realPath))
        self.end_headers()
        self.wfile.write(data)




Is (type, encoding) a tuple? kind of like an array? but what's the reason using like that?

f=codecs.open(path, encoding=encoding) Does it indicates it can open any kind of files, html, pictures, movies..etc and serve up those contents.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: What type of files can this web server serve?

Sun Mar 09, 2014 7:36 am

Have a look at the python docs. This explains that the mime.guess_type method returns a tuple:
http://docs.python.org/2/library/mimetypes.html

I think you could probably answer your last question by running the code and testing it.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”