[mod_python] How to get path from URL

Shawn Harrison harrison at tbc.net
Sun Feb 27 11:52:42 EST 2005


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.
Shawn Harrison


More information about the Mod_python mailing list