Forcing Python to detect DocumentRoot

P

Piet van Oostrum

Ferrous Cranus said:
This is addon domain's counter.py snippet tried to load an image mail.png and failed because it cant see past its document root

========================================
# render html template and print it
data = f.read()
counter = '''<center>
<a href="mailto:[email protected]"> <img src="/data/images/mail.png"> </a>

<table border=2 cellpadding=2 bgcolor=black>
<td><font color=lime> ΑÏιθμός Επισκεπτών </td>
<td><font color=cyan> %d </td>''' % hits[0]
========================================
While from within the same counter.py file

# open html template file
f = open( '/home/nikos/public_html/test.txt' )

opens OK the page file which is also past addons domain's document root

Can you help counter.py to load the image? Why does it fail to load it? Python can have access to ANY filesystempath , no matter from what folder counter.py script runs from. Correct?

That piece of code is not opening the image file. It just issues the URL
for the image file. The file will then be loaded by the browser in a new
request. The image should be at
/home/nikos/public_html/data/images/mail.png

P.S. I don't understand what you mean by "addon domain".
 
A

alex23

When trying to open an html template within Python script i use a relative path to say go one folder back and open index.html

f = open( '../' + page )

How to say the same thing in an absolute way by forcing Python to detect DocumentRoot by itself?

The main ways we handle something like this are:

1. Set an environment variable that Python then reads to get the
DocumentRoot value.
2. Use something like zc.buildout to produce both your httpd.conf and
create a config file containing the value of DocumentRoot.
 
F

Ferrous Cranus

Τη Σάββατο, 19 ΙανουαÏίου 2013 10:01:15 μ.μ. UTC+2, ο χÏήστης Piet van Oostrum έγÏαψε:
This is addon domain's counter.py snippet tried to load an image mail.png and failed because it cant see past its document root


# render html template and print it
data = f.read()
counter = '''<center>
<a href="mailto:[email protected]"> <img src="/data/images/mail.png"> </a>

<table border=2 cellpadding=2 bgcolor=black>
<td><font color=lime> ΑÏιθμός Επισκεπτών </td>
<td><font color=cyan> %d </td>''' % hits[0]
========================================



While from within the same counter.py file
# open html template file
f = open( '/home/nikos/public_html/test.txt' )

opens OK the page file which is also past addons domain's document root

Can you help counter.py to load the image? Why does it fail to load it?Python can have access to ANY filesystempath , no matter from what folder counter.py script runs from. Correct?



That piece of code is not opening the image file. It just issues the URL

for the image file. The file will then be loaded by the browser in a new

request. The image should be at

/home/nikos/public_html/data/images/mail.png

Yes the image is this and is located at that folder.

/home/nikos/public_html/cgi-bin/counter.py

that has embedded this line:

<a href="mailto:[email protected]"> <img src="/data/images/mail.png"> </a>

can open the file normally as seen if you visit http://superhost.gr

P.S. I don't understand what you mean by "addon domain".


While

/home/nikos/public_html/cafebar-idea.gr/cgi-bin/counter.py

that has also embedded this line:

<a href="mailto:[email protected]"> <img src="/data/images/mail.png"> </a>

cannnot open the file normally.

And the questions iw WHY since python script can open ANY filesystempath
file the user has access too.
 
D

Dave Angel

Τη Σάββατο, 19 ΙανουαÏίου 2013 10:01:15 μ.μ. UTC+2, ο χÏήστης Piet van Oostrum έγÏαψε:
While

/home/nikos/public_html/cafebar-idea.gr/cgi-bin/counter.py

that has also embedded this line:

<a href="mailto:[email protected]"> <img src="/data/images/mail.png"> </a>

cannnot open the file normally.

And the questions iw WHY since python script can open ANY filesystempath
file the user has access too.

As Piet has said,Python is NOT opening the file mail.png. When the html
is sent to the browser, and the browser requests that image file, it's
the server itself who figures out where the actual file is. Python
isn't involved at all.
 
F

Ferrous Cranus

Τη ΔευτέÏα, 21 ΙανουαÏίου 2013 2:33:22 μ.μ. UTC+2, ο χÏήστης Dave Angel έγÏαψε:
As Piet has said,Python is NOT opening the file mail.png. When the html

is sent to the browser, and the browser requests that image file, it's

the server itself who figures out where the actual file is. Python

isn't involved at all.

Yes Dave so we need to remove <img src="/data/images/mail.png"> since the apache cant see to open it and let Python open it which we know it can because it has access to any system file the user has access too.

httpd cannot open this file because the location of the image is past the addon domain's Document Root.

/home/nikos/public_html/cafebar-idea.gr = Addon's Domain Document Root

/home/nikos/public_html/data/images/mail.png = where the image file is located

and the python scipt is on:

/home/nikos/public_html/cafebar-idea.gr/cgi-bin/counter.py

So if a python script can open any file the user has access too then we need a "python way" of opening this file.
 
F

Ferrous Cranus

Τη ΔευτέÏα, 21 ΙανουαÏίου 2013 2:33:22 μ.μ. UTC+2, ο χÏήστης Dave Angel έγÏαψε:
As Piet has said,Python is NOT opening the file mail.png. When the html

is sent to the browser, and the browser requests that image file, it's

the server itself who figures out where the actual file is. Python

isn't involved at all.

Yes Dave so we need to remove <img src="/data/images/mail.png"> since the apache cant see to open it and let Python open it which we know it can because it has access to any system file the user has access too.

httpd cannot open this file because the location of the image is past the addon domain's Document Root.

/home/nikos/public_html/cafebar-idea.gr = Addon's Domain Document Root

/home/nikos/public_html/data/images/mail.png = where the image file is located

and the python scipt is on:

/home/nikos/public_html/cafebar-idea.gr/cgi-bin/counter.py

So if a python script can open any file the user has access too then we need a "python way" of opening this file.
 
M

Michael Torrie

Yes my Python scripts exist in a linux web host.

os.environ['HOME'] will indeed give the home directory of the user.

to me /home/nikos/

but i want a variable to point to

/home/nikos/public_html whice is called DocumentRoot.

Not it's not. There is nothing in the operating system that defines this.
is there avariable for that? i can't seem to find any...

Not there's nothing in the operating system that specifies this. This
is a convention that makes sense only to the apache daemon itself. If
your python script is running as a CGI script, then apache will set
environment variables that you can read with the os module. See the
Apache docs for information on this.
 
F

Ferrous Cranus

Ok i see its just a convention.
Can you help on this:

so we need to remove <img src="/data/images/mail.png"> since the apache cant see to open it and let Python open it which we know it can because it has access to any system file the user has access too.

httpd cannot open this file because the location of the image is past the addon domain's Document Root.

/home/nikos/public_html/cafebar-idea.gr = Addon's Domain Document Root

/home/nikos/public_html/data/images/mail.png = where the image file is located

and the python scipt is on:

/home/nikos/public_html/cafebar-idea.gr/cgi-bin/counter.py

So if a python script can open any file the user has access too then we need a "python way" of opening this file.
 
F

Ferrous Cranus

Ok i see its just a convention.
Can you help on this:

so we need to remove <img src="/data/images/mail.png"> since the apache cant see to open it and let Python open it which we know it can because it has access to any system file the user has access too.

httpd cannot open this file because the location of the image is past the addon domain's Document Root.

/home/nikos/public_html/cafebar-idea.gr = Addon's Domain Document Root

/home/nikos/public_html/data/images/mail.png = where the image file is located

and the python scipt is on:

/home/nikos/public_html/cafebar-idea.gr/cgi-bin/counter.py

So if a python script can open any file the user has access too then we need a "python way" of opening this file.
 
M

Michael Torrie

Ok i see its just a convention. Can you help on this:

so we need to remove <img src="/data/images/mail.png"> since the
apache cant see to open it and let Python open it which we know it
can because it has access to any system file the user has access too.

Is this link generated by your python CGI script? If so you'll have to
work out some way for your python script to interact with Apache and ask
it where the document root is.

If this link is in static html, then you simply need to fix your html to
make the link valid. Or maybe you need to modify your apache
installation so that it knows where "/data" is using an alias directive
in your apache config.
 
M

Michael Torrie

Yes Dave so we need to remove <img src="/data/images/mail.png">
since the apache cant see to open it and let Python open it which we
know it can because it has access to any system file the user has
access too.

What are you trying to accomplish? I don't see how opening the file
with python will do anything because as has been said many times on this
thread, your python CGI generates html code which the browser then
renders. Opening an image file with python will do nothing useful.
So if a python script can open any file the user has access too then
we need a "python way" of opening this file.

Still don't understand why you want python to open the image file. What
do you want python to do with it? It's running on a web server, so
there's no screen for python to display the image too.

Technically it is possible to have a script that opens the image and
serves it up as a binary stream to the browser using the image
content-type header, but it's way more efficient to serve up the file
statically. And you'd have to have a proper link in the html code
anyway, to refer to your image-serving CGI script.

Methinks you're barking up the wrong tree with python opening the image
file.
 
M

Michael Torrie

# render html template and print it data = f.read() counter =
'''<center> <a href="mailto:[email protected]"> <img
src="/data/images/mail.png"> </a>

<table border=2 cellpadding=2 bgcolor=black> <td><font color=lime>
ΑÏιθμός Επισκεπτών </td> <td><font color=cyan> %d </td>''' % hits[0]
========================================

While from within the same counter.py file

# open html template file f = open(
'/home/nikos/public_html/test.txt' )

opens OK the page file which is also past addons domain's document
root

Can you help counter.py to load the image? Why does it fail to load
it? Python can have access to ANY filesystempath , no matter from
what folder counter.py script runs from. Correct?

No I can't because counter.py doesn't "load the image." The browser
does. If the image fails to load it is because the apache web server
cannot find it. In other words your image src url is bad. It has
nothing to do with python. Python is only spitting out html code.
That's it. Image loading is done by apache on behalf of a request from
the web browser. Since the url is a direct url to a file, there is no
CGI that runs.

I understand that you have a difficulty understanding the relationship
between the browser, the web server, and the cgi script. The process
goes like this:

- browser requests the url, which happens to be the CGI script, counter.py.
- web server runs counter.py returns html code to the browser.
- browser parses html code, renders it, and requests any images that the
html code references.
- Web server tries to locate the image based on its own rules and
config, and serves it if possible, otherwise, returns error 404.

So you simply have the image url wrong. apache is not mapping /data to
where you think it is. You have to either fix this in apache's configs,
or determine where the image really is in apache's url space, and change
the cgi to output the correct html. Your problem isn't a python one at
all; it's an apache problem.
 
P

Piet van Oostrum

Ferrous Cranus said:
Ok i see its just a convention.
Can you help on this:

so we need to remove <img src="/data/images/mail.png"> since the apache cant see to open it and let Python open it which we know it can because it has access to any system file the user has access too.

httpd cannot open this file because the location of the image is past the addon domain's Document Root.

/home/nikos/public_html/cafebar-idea.gr = Addon's Domain Document Root

/home/nikos/public_html/data/images/mail.png = where the image file is located

and the python scipt is on:

/home/nikos/public_html/cafebar-idea.gr/cgi-bin/counter.py

So if a python script can open any file the user has access too then we need a "python way" of opening this file.

So why don't you put the image at /home/nikos/public_html/cafebar-idea.gr/data/images/mail.png?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top