[Tutor] How can a CGI program get the URL that called it?
Terry Carroll
carroll at tjc.com
Fri Jan 10 23:44:35 CET 2014
Ah, I discovered what my problem was...
On Fri, 10 Jan 2014, Alan Gauld wrote:
> its calling your file. You should know where your file is?
My problem was that, I know where the file is in the host's file system,
and relative to my CGI program. I do not have a URL to that file.
> If you want to create a png file and display it to the user then you
> just store the file somewhere in your web site and create an html file
> that has an img tag referencing that location.
Right; I am producing HTML output (using the print command, not as a
file), with an img tag. The img tag has a src attribute, which must
provide the URL of the referenced image file.
But I do not have that URL. I know where that file is in the file system,
and relative to my CGI program. But I do not have a URL to the file. My
thinking was that, if I have the URL to my program, it's pretty trivial to
construct the URL to the file.
And here's where my real problem was: I had tried specifying just a
relative path in the src tag, and that did not work consistently;
specifically, I found that it worked in Chrome, but not Firefox.
As it turns out, since I was testing on a Windows box, os.path.relpath was
(reasonably) using a '\' as the separator character (surprisingly, so does
posixpath.relpath). By simply adding:
relative_path = relative_path.replace('\\', '/')
It uses the '/' required in a URL (even a relative-path URL) and works.
Chrome was forgiving of the '\'; other browsers not so much.
It was not until I posted the <img> tag into a draft of this reply that I
noticed the '\' characters.
I swear I looked at this for hours without noticing this before.
More information about the Tutor
mailing list