how to serve a simple file using tornado

3,384 views
Skip to first unread message

aliane abdelouahab

unread,
Nov 18, 2012, 4:16:37 PM11/18/12
to Tornado Web Server
hi
ok sorry for this dumb question that i've messed in my first tutorial!
i've jumbed directly to mongodb using this:

class PictureHandler(BaseHandler):
def get(self,picture):
profile = self.fs.get_last_version(picture)
if profile:
header = "image/%s" % profile.content_type
self.add_header("Content-Type",header)
self.write(profile.read())

so how do i use a handler to serve a picture from a folder (without
using mongodb, just simple os directory) that i create dynamically
(every user has his own directory, for example /my-app/users/
picture125.jpg )

note that it's not in static files, i keep users outside, is it a good
idea?

Jordon Mears

unread,
Nov 18, 2012, 9:50:34 PM11/18/12
to python-...@googlegroups.com
You can create you own static file handler and map the path in your application to allow a user to access their folder.

application = tornado.web.Application([
    (r'/my-app/(.*)', tornado.web.StaticFileHandler,
     {path='/path/on/disk'}),
], **settings)
Then each user could have their own folder inside your app.

/path/on/disk/user1
/path/on/disk/user2

NOTE: These folders for each user are not secure and could be accessed by anyone.
--
Jordon Mears
http://www.finefrog.com

aliane abdelouahab

unread,
Nov 19, 2012, 4:47:42 AM11/19/12
to Tornado Web Server
thank you :D
and what about the method of creating a handler and then using the url
spec to invoke it:

what i want to do exactly is:

url specs:
(r"/{0}/".format(os.path.join(os.path.dirname(__file__), "users")),
handlers.PictureHandler),

class PictureHandler(BaseHandler):
def get(self,picture):
nofich = path.join(path.dirname(__file__), "users")
if nofich:
header = "image/jpeg"
self.add_header("Content-Type",header)
print nofich.read()
self.write(nofich.read())

this handler is never called!

what i try to accomplish is:

if by using chrome inspect i try to verify picture url, it is:
http://localhost:8000/G:/Myprojet/essog/users/abdelou...@yahoo.fr/135325895941-cat.jpg

and from windows it is:
file:///G:/Myprojet/essog/users/abdelou...@yahoo.fr/135325895941-cat.jpg

and about access, if it is under a POSIX machines, it will not make
some restrictions of accessing the file, only tornado can get them,
since tornado is used by a SU no?

On 19 nov, 03:50, Jordon Mears <jordo...@gmail.com> wrote:
> You can create you own static file handler and map the path in your
> application to allow a user to access their folder.
>
> application = tornado.web.Application([
>     (r'/my-app/(.*)', tornado.web.StaticFileHandler,
>      {path='/path/on/disk'}),
> ], **settings)
>
> Then each user could have their own folder inside your app.
>
> /path/on/disk/user1
> /path/on/disk/user2
>
> NOTE: These folders for each user are not secure and could be accessed by
> anyone.
>
> On Sun, Nov 18, 2012 at 4:16 PM, aliane abdelouahab <alabdeloua...@gmail.com

aliane abdelouahab

unread,
Nov 19, 2012, 4:55:40 AM11/19/12
to Tornado Web Server
of course the first link dont work

http://localhost:8000/G:/Myprojet/essog/users/abdelou...@yahoo.fr/135325895941-cat.jpg

as from what i understand when there is locahost it delegates images
servers to tornado, and tornado dont find its url spec equivalent, so
i get 404

On 19 nov, 10:47, aliane abdelouahab <alabdeloua...@gmail.com> wrote:
> thank you :D
> and what about the method of creating a handler and then using the url
> spec to invoke it:
>
> what i want to do exactly is:
>
> url specs:
> (r"/{0}/".format(os.path.join(os.path.dirname(__file__), "users")),
> handlers.PictureHandler),
>
> class PictureHandler(BaseHandler):
>     def get(self,picture):
>         nofich = path.join(path.dirname(__file__), "users")
>         if nofich:
>             header = "image/jpeg"
>             self.add_header("Content-Type",header)
>             print nofich.read()
>             self.write(nofich.read())
>
> this handler is never called!
>
> what i try to accomplish is:
>
> if by using chrome inspect i try to verify picture url, it is:
> http://localhost:8000/G:/Myprojet/essog/users/abdelouahab...@yahoo.fr/135325895941-cat.jpg
>
> and from windows it is:
> file:///G:/Myprojet/essog/users/abdelouahab...@yahoo.fr/135325895941-cat.jpg

aliane abdelouahab

unread,
Nov 20, 2012, 10:34:56 AM11/20/12
to Tornado Web Server
it seems that i dident understand the concept of the regular
expression (the dot to mean everything end with dot, the pictures in
this case) :p
now it works and without the handler, since we used directly a tornado
metho.
thank you again :)

On 19 nov, 03:50, Jordon Mears <jordo...@gmail.com> wrote:
> You can create you own static file handler and map the path in your
> application to allow a user to access their folder.
>
> application = tornado.web.Application([
>     (r'/my-app/(.*)', tornado.web.StaticFileHandler,
>      {path='/path/on/disk'}),
> ], **settings)
>
> Then each user could have their own folder inside your app.
>
> /path/on/disk/user1
> /path/on/disk/user2
>
> NOTE: These folders for each user are not secure and could be accessed by
> anyone.
>
> On Sun, Nov 18, 2012 at 4:16 PM, aliane abdelouahab <alabdeloua...@gmail.com
Reply all
Reply to author
Forward
0 new messages