[mod_python] How to get path from URL

Graham Dumpleton grahamd at dscpl.com.au
Sun Feb 27 16:04:04 EST 2005

On 28/02/2005, at 3:52 AM, Shawn Harrison wrote:

> Gerald Lee wrote [02/27/05 1:54 AM]:
>> The url is: http://localhost/ebiz/admin/index.py/index
>> I can get string "http://localhost/ebiz/admin/", who can help me?
> I got curious, so I came up with the following:
> i = len(req.document_root())
> path = req.filename[i+1:]	# +1 is for the fwd slash
> p = 'http://' + '/'.join([req.hostname, path])
> # same thing, functional style
> p = ('http://'
>     + '/'.join([req.hostname, 
> req.filename.strip('/')[len(req.document_root().strip('/'))+1:]]))
> This makes no provision for the case where uri = req.hostname.

Don't understand your last comment, but I don't believe what you have 
always work. The problem is that you use the physical filesystem path 
This will fail for ~user access and possibly other cases where a 
physical directory is interposed into an arbitrary location in the URL

Take the following for example:

   req.filename = /Users/grahamd/Sites/hclass/page
   req.document_root() = /usr/local/apache-2.0/htdocs
   req.uri = /~grahamd/hclass/page/method2
   req.unparsed_uri = /~grahamd/hclass/page/method2
   req.path_info = /method2

Your code yields:


That it got so close was purely because virtual URL was similar length
to physical path name.

What you want is:

   p = 'http://' + req.hostname + req.uri[:-len(req.path_info)]

Which yields:


This will not work though if not the standard server port or if "https" 

One is therefore better off trying to work out the relative location to 
directory and avoid absolute URLs if possible.


More information about the Mod_python mailing list