[mod_python] Server address

Graham Dumpleton graham.dumpleton at gmail.com
Sun Aug 31 19:32:39 EDT 2008


2008/9/1 Tiago Becker <tiagobecker at gmail.com>:
> Oh, cool, i didnt know that req.construct_url() would do the trick, thanks a
> lot!
>
>> In web applications I would regard it as bad practice to hard wire
>> into configuration files URLs for the same site which incorporate the
>> name of the site, which seems to be what you are doing here.
>
>
> Ok, i can take your word for that, but could you elaborate on why is it a
> bad practice? Since the url would be supplied (spelling?) by apache itself.
> I dont see whats wrong with that.

If you are doing it for all HTML href attributes of link elements for
inter site links it is bad because it prohibits ability to save down a
related set of pages from a site and then browse them offline. If the
links are just path relative, then this would work.

That is just one example. Not knowing exactly how you are using them,
hard to comment more.

Graham

> It's not that i would write myVar = 'http://hostname etc' on my cfg (this i
> can see it would be wrong)
>
> on php i would do myVar = $_SERVER['hostname'] or something like it
>
> I just want to know why because I don't understand your reasons for saying
> that... Maybe i coded in php for far too long :-)
>
> Sorry about the reply stuff and thank you for you patience :-D
>
>
>
>
> On Sun, Aug 31, 2008 at 6:56 AM, Graham Dumpleton
> <graham.dumpleton at gmail.com> wrote:
>>
>> 2008/8/31 Graham Dumpleton <graham.dumpleton at gmail.com>:
>> > ---------- Forwarded message ----------
>> > From: Tiago Becker <tiagobecker at gmail.com>
>> > Date: 2008/8/31
>> > Subject: Re: [mod_python] Server address
>> > To: Graham Dumpleton <graham.dumpleton at gmail.com>
>> >
>> >
>> > Well, i will probably use more than one .py file... I dont want to
>> > copy all the configuration on each file...
>> > Besides, i work with 5 other developers, each one with their own
>> > virtual server and having to configurate every developer enviroment
>> > seems too much trouble to maintain..
>> >
>> > Well, i could import my CFG file inside the function, that would make
>> > the request available (i think :-))... i just wanted to know if there
>> > was any other way to do it.
>> >
>> > The fact is that im coming from php where i had a global cfg file for
>> > all the url configurations.. All i want with that is to have a config
>> > file that will get the urls (and others variables) needed so no one
>> > has to worry about that.
>>
>> PHP is hardly the indicator of good practice.
>>
>> In web applications I would regard it as bad practice to hard wire
>> into configuration files URLs for the same site which incorporate the
>> name of the site, which seems to be what you are doing here.
>>
>> The configuration file can contain an absolute path to a page, but it
>> should not be a fully formed HTTP address. Any code generating a
>> response which requires a fully formed HTTP address should take that
>> absolute path and feed it into req.construct_url(). This maps to an
>> internal Apache function which will fill it out to be a fully formed
>> HTTP address with all the correct hostname and port information as
>> matches what the original request to the site used.
>>
>> In other words, if you do things the correct way of using
>> req.construct_url() you don't need to hardwire your hostname into your
>> global configuration files.
>>
>> If for some reason the code generating the response doesn't have
>> access to the request object, then you should be looking at Python
>> features such as thread locals to stash away some request information
>> that can then be accessed by code directly which otherwise didn't get
>> passed the request object. This would allow it then to get hold of the
>> request object and call the construct_url() function.
>>
>> That all said, why do you even need a fully formed HTTP address? If
>> you are including that in pages for the same site then that also could
>> be considered bad practice. You should just use an absolute, or better
>> still a relative path, in any href attributes of a HTML anchor tag,
>> not a full formed HTTP address.
>>
>> > Re-reading my post, i saw that i wasnt clear enough :-)
>>
>> I knew what you wanted. I questioned it because it seemed to be a poor
>> way of doing it.
>>
>> BTW, please use reply-all and keep followups on mailing list.
>>
>> Graham
>>
>> > Thanks for you answers and your patience with my 'english' :-)
>> >
>> >
>> > On Fri, Aug 29, 2008 at 8:47 PM, Graham Dumpleton
>> > <graham.dumpleton at gmail.com> wrote:
>> >>
>> >> 2008/8/30 Tiago Becker <tiagobecker at gmail.com>:
>> >> > Hello guys!
>> >> >
>> >> > Im trying to make my own framework for mod python, because none of
>> >> > the
>> >> > available suited my needs.
>> >> >
>> >> > Im trying to get the server address, without the request object, but
>> >> > i
>> >> > couldnt find in the documentation.. is that possible?
>> >> >
>> >> >
>> >> > Heres why i need it: i import apache and a configuration file, but
>> >> > that
>> >> > config need to know the server address.. as i import the files prior
>> >> > to the
>> >> > function that handles the request, i dont have the req object...
>> >> >
>> >> > from mod_python import apache
>> >> > import sys, os
>> >> >
>> >> > #my cfg
>> >> > CFG = apache.import_module('cfg/cfg')
>> >> > CFG.LOCAL_PATH = os.path.dirname( __file__ )
>> >> >
>> >> > ..other imports etc
>> >> >
>> >> > def index(req):
>> >> >     #this is ther var i would like to get outside the function
>> >> >     CFG.URL = 'http://' + req.hostname
>> >>
>> >> Sounds like you are going about it the wrong way. Can you give a
>> >> better reason why you need it.
>> >>
>> >> First off, the only server details you can get outside of the context
>> >> of a request are those for the main server. If requests are handled
>> >> within the context of a VirtualHost then you can't do it. For main
>> >> server details do:
>> >>
>> >>  from mod_python import apache
>> >>  print apache.main_server.server_hostname
>> >>
>> >> Documentation at:
>> >>
>> >>  http://www.modpython.org/live/current/doc-html/pyapi-apmem.html
>> >>  http://www.modpython.org/live/current/doc-html/pyapi-mpserver.html
>> >>  http://www.modpython.org/live/current/doc-html/pyapi-mpsrv-mem.html
>> >>
>> >> If you need to construct URLs for use inside of a request, you should
>> >> use:
>> >>
>> >>  req.construct_url('/some/path')
>> >>
>> >> Documetation at:
>> >>
>> >>
>> >>  http://www.modpython.org/live/current/doc-html/pyapi-mprequest-meth.html
>> >>
>> >> Graham
>> >
>
>


More information about the Mod_python mailing list