how to translate a virtual path to a physical path in a CGI script

raymondjia raymondjiaNOraSPAM at yahoo.com.invalid
Wed Oct 6 13:09:53 EDT 1999


Thanks for your reply.

Currently I am running IIS, which provides PATH_INFO and
PATH_TRANSLATED and so on.  So I have no problem getting to know the
physical path for the script itself.  What I really need is a function
that given *any* virtual path, it can translate it into a physical one.

I managed to write a mappath() function yesterday, which substracts
PATH_INFO from PATH_TRANSLATED to derive the document root, and join
the given logial path to it.  This way is definitely not reliable, is
it?

Here it is:
#
import urllib, string
from os import *

def mappath(virtual_path):
	if virtual_path[0] == '/':		
		self_virt = urllib.url2pathname(environ['PATH_INFO'])
		self_xlated = environ['PATH_TRANSLATED']
		pos = string.rfind(self_xlated, self_virt)
		doc_root = self_xlated[0:pos]
		return path.join(doc_root, urllib.url2pathname(virtual_path[1:]))
	else:
		return path.join(getcwd(), urllib.url2pathname(virtual_path))	
#

raymond

In article <7tfept$lkc$1 at news.kolumbus.fi>,
torppa at polykoira.megabaud.fi (Jarkko Torppa) wrote:
> In article <000b8d9b.daa24cc0 at usw-ex0102-015.remarq.com>,
> raymondjia wrote:
> >Hi, all
> >
> >Can anybody tell me how to translate a virtual path to a physical
> one
> >in a CGI script?  I browsed through the recent topics and
> couldn't find
> >the answer.  Basicly what I need is the equivalent of ASP's
> >Server.MapPath().
> >
> >Is there any Python CGI FAQ page around?
> This is not python specific, it is more specific to your
> webservers cgi
> implementation.
> apache on unix sets the following environment variables that have
> the infomation
> you seem to be looking for.
> PATH_INFO
>        /haloo
> PATH_TRANSLATED
>        /www/htdocs/haloo
> QUERY_STRING
> REQUEST_URI
>        /~torppa/koe.cgi/haloo
> SCRIPT_FILENAME
>        /homes/polykoira/torppa/.public_html/koe.cgi
> SCRIPT_NAME
>        /~torppa/koe.cgi
> the request was for /~torppa/koe.cgi/halloo
> /~torppa/koe.cgi is the cgi-bin.
> --
>  Jarkko Torppa                torppa at staff.megabaud.fi
>   Megabaud Internet-palvelut



* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!





More information about the Python-list mailing list