Serving dynamic HTML files

55 views
Skip to first unread message

John Duddy

unread,
Nov 1, 2011, 3:08:10 PM11/1/11
to web2py-users
I'm creating an app that runs external programs, which produce HTML
reports. These reports are produced outside the static directory, but
I want the user to be able to browse them.

Is there a way I can tell web2py to serve those files like it does
static files? All the links in the HTML files are relative, so if I
can create a URL that web2py will use for the base HTML file, I think
it should work.

This is an intranet application, so security is not a big concern.

Thanks!

Richard Vézina

unread,
Nov 1, 2011, 3:17:44 PM11/1/11
to web...@googlegroups.com
I think this is what you are looking for : http://web2py.com/book/default/chapter/12#Fetching-an-external-URL

Richard

Anthony

unread,
Nov 1, 2011, 3:42:51 PM11/1/11
to web...@googlegroups.com
On Tuesday, November 1, 2011 3:08:10 PM UTC-4, John Duddy wrote:
I'm creating an app that runs external programs, which produce HTML
reports. These reports are produced outside the static directory, but
I want the user to be able to browse them.

Do you have control over where the reports are placed -- that is, can you save them in the /static folder of your app?

John Duddy

unread,
Nov 1, 2011, 5:46:57 PM11/1/11
to web2py-users
That lets me read the URL from my web2py code, right? Instead, I want
to serve the file via web2py to the browser.

I suppose I could use this with response.stream, if I knew how to
parse the rest of the URL component below my controller....

On Nov 1, 12:17 pm, Richard Vézina <ml.richard.vez...@gmail.com>
wrote:

John Duddy

unread,
Nov 1, 2011, 5:48:12 PM11/1/11
to web2py-users
Unfortunately, no. I might be able to create soft links under static
to those directories, making up a guid each time for the name. I was
hoping for a more elegant solution.

Anthony

unread,
Nov 1, 2011, 10:54:11 PM11/1/11
to web...@googlegroups.com
On Tuesday, November 1, 2011 5:48:12 PM UTC-4, John Duddy wrote:
Unfortunately, no. I might be able to create soft links under static
to those directories, making up a guid each time for the name. I was
hoping for a more elegant solution.

If these are purely static files, why do you need web2py to serve them? Can't you set up your web server to serve them from where they are, or do you only have web2py's built-in server available (if so, maybe you can run a separate instance of Rocket using the fs method: http://packages.python.org/rocket/methods.html#fs)?


Anthony

unread,
Nov 1, 2011, 10:55:57 PM11/1/11
to web...@googlegroups.com
On Tuesday, November 1, 2011 5:46:57 PM UTC-4, John Duddy wrote:
That lets me read the URL from my web2py code, right? Instead, I want
to serve the file via web2py to the browser.

I suppose I could use this with response.stream, if I knew how to
parse the rest of the URL component below my controller....

response.stream() takes an open file object, so I suppose you could do open('path/to/file', 'rb') to open the file for streaming.

Anthony 

~redShadow~

unread,
Nov 1, 2011, 11:07:56 PM11/1/11
to web...@googlegroups.com
On Tue, 2011-11-01 at 19:54 -0700, Anthony wrote:
> If these are purely static files, why do you need web2py to serve
> them? Can't you set up your web server to serve them from where they
> are, or do you only have web2py's built-in server available (if so,
> maybe you can run a separate instance of Rocket using the fs
> method: http://packages.python.org/rocket/methods.html#fs)?

+1

Plus, if you are running web2py via apache, my suggestion is something
like this:

- Configure an alias (``Alias /reports /path/to/reports``) in the
virtualhost configuration
- Configure in your app that ``reports_url = "/reports/"`` and
``reports_path = "/path/to/reports"``
- Browse files into reports_path and then link them using
``posixpath.join(reports_url, 'path/to/file')``

[posixpath uses UNIX file names, so it joins using '/'; only on Unix
platforms (a part from macosx..?) os.path == posixpath]
--
Samuele ~redShadow~ Santi
----------------------------------------------------------------
redshadow[at]hackzine.org - redshadowhack[at]gmail.com

Blog: http://hackzine.org

GPG Key signature:
050D 3E9F 6E0B 44CE C008 D1FC 166C 3C7E EB26 4933
----------------------------------------------------------------
/me recommends:
Squadra Informatica - http://www.squadrainformatica.com
----------------------------------------------------------------
- Proud ThinkPad T-Series owner
- Registered Linux-User: #440008
* GENTOO User since 1199142000 (2008-01-01)
* former DEBIAN SID user
----------------------------------------------------------------
"Software is like sex: it's better when it's free!"
-- Linus Torvalds

signature.asc

John Duddy

unread,
Nov 2, 2011, 11:53:05 AM11/2/11
to web2py-users
These files can appear ANYWHERE on the file system. I do not know in
advance, as the user can add directories for the input (and output)
data at runtime via configuration. So, I need a fully dynamic solution
that I can tweak programmatically at runtime.

Your solution (creating links under a path served by Apache) is
similar to something I already tried (a path under static/).

What I'd really love is to be able to have a routes module I could
talk to from the controllers, or one that called a function in my
controller to get the file name to read given a URL.
>     Squadra Informatica -http://www.squadrainformatica.com
> ----------------------------------------------------------------
>  - Proud ThinkPad T-Series owner
>  - Registered Linux-User: #440008
>       * GENTOO User since 1199142000 (2008-01-01)
>       * former DEBIAN SID user
> ----------------------------------------------------------------
>       "Software is like sex: it's better when it's free!"
>                               -- Linus Torvalds
>
>  signature.asc
> < 1KViewDownload

John Duddy

unread,
Nov 2, 2011, 12:25:58 PM11/2/11
to web2py-users
Certainly there is s simpler way to talk to an already running
instance of rocket (mine) and tell it to serve more static files? I
could also do it via apache, if I could talk to apache and tell it to
serve from locations not listed in its config. I need to do this at
runtime, though.

Anthony

unread,
Nov 2, 2011, 12:39:11 PM11/2/11
to web...@googlegroups.com
Well, there's always this option: https://groups.google.com/d/msg/web2py/_GohKTJhn5c/KkeQzH1xRf0J


On Wednesday, November 2, 2011 11:53:05 AM UTC-4, John Duddy wrote:
These files can appear ANYWHERE on the file system. I do not know in
advance, as the user can add directories for the input (and output)
data at runtime via configuration. So, I need a fully dynamic solution
that I can tweak programmatically at runtime.

Your solution (creating links under a path served by Apache) is
similar to something I already tried (a path under static/).

What I'd really love is to be able to have a routes module I could
talk to from the controllers, or one that called a function in my
controller to get the file name to read given a URL.

~redShadow~

unread,
Nov 2, 2011, 1:03:26 PM11/2/11
to web...@googlegroups.com
On Wed, 2011-11-02 at 09:25 -0700, John Duddy wrote:
> Certainly there is s simpler way to talk to an already running
> instance of rocket (mine) and tell it to serve more static files? I
> could also do it via apache, if I could talk to apache and tell it to
> serve from locations not listed in its config. I need to do this at
> runtime, though.

To serve anything anywhere in the filesystem, you can do something like:

Alias /reports /

And then append the file path. Of course be aware of the risks of
letting users browse the whole filesystem..

If that's in a small intranet to which only trusted people can have
access, plus you tweak permissions on filesystem in order to limit where
the www-data user can have access, plus limit cgi/php/.. file execution
to only some locations, and maybe even add http authentication, this
could be a valid solution..

Better if you can restrict more the accessible path, eg. if your users
will create reports only in their home dirs, you can use something like:

Alias /reports /home

And maybe even limit which user can access which directory (there are
several ways to do this, depending on where you get the users, etc.)

--
Samuele ~redShadow~ Santi
----------------------------------------------------------------
redshadow[at]hackzine.org - redshadowhack[at]gmail.com

Blog: http://hackzine.org

GPG Key signature:
050D 3E9F 6E0B 44CE C008 D1FC 166C 3C7E EB26 4933
----------------------------------------------------------------
/me recommends:

Squadra Informatica - http://www.squadrainformatica.com

signature.asc

John Duddy

unread,
Nov 2, 2011, 2:04:39 PM11/2/11
to web2py-users
That's cool - I'll definitely consider it. Thanks!
>     Squadra Informatica -http://www.squadrainformatica.com
> ----------------------------------------------------------------
>  - Proud ThinkPad T-Series owner
>  - Registered Linux-User: #440008
>       * GENTOO User since 1199142000 (2008-01-01)
>       * former DEBIAN SID user
> ----------------------------------------------------------------
>       "Software is like sex: it's better when it's free!"
>                               -- Linus Torvalds
>
>  signature.asc
> < 1KViewDownload
Reply all
Reply to author
Forward
0 new messages