Forcing Python to detect DocumentRoot

F

Ferrous Cranus

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?
 
F

Ferrous Cranus

Τη ΤετάÏτη, 16 ΙανουαÏίου 2013 4:01:07 μ.μ. UTC+2, ο χÏήστης Joel Goldstick έγÏαψε:
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?

--

http://mail.python.org/mailman/listinfo/python-list



I don't think I understand your question.  But, I think your answer is here:

Nowhere that page says something about python detecting documentroot directory (www or public_html) that is.
 
F

Ferrous Cranus

Document Root for me is /home/nikos/public_html
Is where Apache store the user www files.

How to tell it my using a variable?
 
R

rusi

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?

Is this what you want?
import os
os.getcwd()
 
R

Roy Smith

Ferrous Cranus said:
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?

Can you give us more details of what you're doing. Is there some web
framework you're using? Can you post some code that's not working for
you?
 
J

Joel Goldstick

Can you give us more details of what you're doing. Is there some web
framework you're using? Can you post some code that's not working for
you?

Import os

Then read os.environ['HOME']

This will give you the home directory of the user. in my case:

This is probably linux only, but that seems to be the environment you are
working in .
 
F

Ferrous Cranus

Τη Πέμπτη, 17 ΙανουαÏίου 2013 5:14:19 μ.μ. UTC+2, ο χÏήστης Joel Goldstick έγÏαψε:
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?



Can you give us more details of what you're doing.  Is there some web

framework you're using?  Can you post some code that's not working for

you?

--

http://mail.python.org/mailman/listinfo/python-list



Import os

Then read os.environ['HOME']


This will give you the home directory of the user.  in my case:

os.environ['HOME'] '/home/jcg'


This is probably linux only, but that seems to be the environment you areworking in .

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.

is there avariable for that? i can't seem to find any...
 
F

Ferrous Cranus

Τη Πέμπτη, 17 ΙανουαÏίου 2013 5:14:19 μ.μ. UTC+2, ο χÏήστης Joel Goldstick έγÏαψε:
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?



Can you give us more details of what you're doing.  Is there some web

framework you're using?  Can you post some code that's not working for

you?

--

http://mail.python.org/mailman/listinfo/python-list



Import os

Then read os.environ['HOME']


This will give you the home directory of the user.  in my case:

os.environ['HOME'] '/home/jcg'


This is probably linux only, but that seems to be the environment you areworking in .

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.

is there avariable for that? i can't seem to find any...
 
J

Joel Goldstick

Τη Πέμπτη, 17 ΙανουαÏίου 2013 5:14:19 μ.μ. UTC+2, ο χÏήστης Joel Goldstick
έγÏαψε:
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?



Can you give us more details of what you're doing. Is there some web

framework you're using? Can you post some code that's not working for

you?

--

http://mail.python.org/mailman/listinfo/python-list



Import os

Then read os.environ['HOME']


This will give you the home directory of the user. in my case:

os.environ['HOME'] '/home/jcg'


This is probably linux only, but that seems to be the environment you
are working in .

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.

is there avariable for that? i can't seem to find any...


DocumentRoot = os.environ['HOME'] + 'public_html'
 
R

Rodrick Brown

Ôç ÐÝìðôç, 17 Éáíïõáñßïõ 2013 5:14:19ì.ì. UTC+2, ï ÷ñÞóôçò Joel Goldstick
Ýãñáøå:

$ export DOCUMENT_ROOT=${HOME}/public _html

Then from python os.environ['DOCUMENT_ROOT'] will have the relative path.

I hope this helps.

Can you give us more details of what you're doing. Is there some web

framework you're using? Can you post some code that's not working for

you?

--

http://mail.python.org/mailman/listinfo/python-list



Import os

Then read os.environ['HOME']


This will give you the home directory of the user. in my case:

os.environ['HOME'] '/home/jcg'


This is probably linux only, but that seems to be the environment you
are working in .

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.

is there avariable for that? i can't seem to find any...
 
F

Ferrous Cranus

Τη ΠαÏασκευή, 18 ΙανουαÏίου 2013 3:28:10 μ.μ. UTC+2, ο χÏήστης Joel Goldstick έγÏαψε:
DocumentRoot = os.environ['HOME'] + 'public_html'

Yes, iam using this and it works.
One last thing:

my python script file is located at /home/nikos/public_html/addon_domain/cgi-bin/

How python is able to run the following statement?

f = open( '/home/nikos/public_html/' + page )

which is clearly levels up of addon domain's DocumentRoot?
 
F

Ferrous Cranus

Τη ΠαÏασκευή, 18 ΙανουαÏίου 2013 3:28:10 μ.μ. UTC+2, ο χÏήστης Joel Goldstick έγÏαψε:
DocumentRoot = os.environ['HOME'] + 'public_html'

Yes, iam using this and it works.
One last thing:

my python script file is located at /home/nikos/public_html/addon_domain/cgi-bin/

How python is able to run the following statement?

f = open( '/home/nikos/public_html/' + page )

which is clearly levels up of addon domain's DocumentRoot?
 
J

Joel Goldstick

Τη ΠαÏασκευή, 18 ΙανουαÏίου 2013 3:28:10 μ.μ. UTC+2, ο χÏήστης Joel
Goldstick έγÏαψε:
DocumentRoot = os.environ['HOME'] + 'public_html'

Yes, iam using this and it works.
One last thing:

my python script file is located at
/home/nikos/public_html/addon_domain/cgi-bin/

How python is able to run the following statement?

f = open( '/home/nikos/public_html/' + page )

which is clearly levels up of addon domain's DocumentRoot?

My website experience with python has been using mod wsgi (and django), not
cgi. I can't help you with how to configure cgi, but googling python cgi
might help
 
F

Ferrous Cranus

Yes, iam using this and it works.
One last thing:

my python script file is located at /home/nikos/public_html/addon_domain/cgi-bin/

How python is able to run the following statement?

f = open( '/home/nikos/public_html/' + page )

which is clearly levels up of addon domain's DocumentRoot?
 
F

Ferrous Cranus

Yes, iam using this and it works.
One last thing:

my python script file is located at /home/nikos/public_html/addon_domain/cgi-bin/

How python is able to run the following statement?

f = open( '/home/nikos/public_html/' + page )

which is clearly levels up of addon domain's DocumentRoot?
 
C

Chris Angelico

Ôç ÐáñáóêåõÞ, 18 Éáíïõáñßïõ 2013 3:28:10 ì.ì. UTC+2, ï ÷ñÞóôçò Joel Goldstick Ýãñáøå:
DocumentRoot = os.environ['HOME'] + 'public_html'

Yes, iam using this and it works.
One last thing:

my python script file is located at /home/nikos/public_html/addon_domain/cgi-bin/

How python is able to run the following statement?

f = open( '/home/nikos/public_html/' + page )

which is clearly levels up of addon domain's DocumentRoot?

Time to take a step backward and figure out what you're really trying
to accomplish. I think, after gazing idly into my crystal ball for a
while, that you actually want to chroot your script - instead of
seeing "/home/nikos/public_html/" it would see just "/", and then it
can't access anything outside of that.

ChrisA
 
F

Ferrous Cranus

Τη ΠαÏασκευή, 18 ΙανουαÏίου 2013 11:34:05 μ.μ. UTC+2, ο χÏήστης Chris Angelico έγÏαψε:
Τη ΠαÏασκευή, 18ΙανουαÏίου 2013 3:28:10 μ.μ. UTC+2, ο χÏήστης Joel Goldstick έγÏαψε:
DocumentRoot = os.environ['HOME'] + 'public_html'
Yes, iam using this and it works.
One last thing:

my python script file is located at /home/nikos/public_html/addon_domain/cgi-bin/

How python is able to run the following statement?

f = open( '/home/nikos/public_html/' + page )

which is clearly levels up of addon domain's DocumentRoot?



Time to take a step backward and figure out what you're really trying

to accomplish. I think, after gazing idly into my crystal ball for a

while, that you actually want to chroot your script - instead of

seeing "/home/nikos/public_html/" it would see just "/", and then it

can't access anything outside of that.



ChrisA


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?
 
F

Ferrous Cranus

Τη ΠαÏασκευή, 18 ΙανουαÏίου 2013 11:34:05 μ.μ. UTC+2, ο χÏήστης Chris Angelico έγÏαψε:
Τη ΠαÏασκευή, 18ΙανουαÏίου 2013 3:28:10 μ.μ. UTC+2, ο χÏήστης Joel Goldstick έγÏαψε:
DocumentRoot = os.environ['HOME'] + 'public_html'
Yes, iam using this and it works.
One last thing:

my python script file is located at /home/nikos/public_html/addon_domain/cgi-bin/

How python is able to run the following statement?

f = open( '/home/nikos/public_html/' + page )

which is clearly levels up of addon domain's DocumentRoot?



Time to take a step backward and figure out what you're really trying

to accomplish. I think, after gazing idly into my crystal ball for a

while, that you actually want to chroot your script - instead of

seeing "/home/nikos/public_html/" it would see just "/", and then it

can't access anything outside of that.



ChrisA


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?
 
B

Barry Scott

When trying to open an html template within Python script i use a relativepath 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?

In the general case it is not possible. There is no single convention you can use to detect the top of a web site.

If you always use apache httpd then read the value of DocumentRoot from httpd.conf

Barry
 

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,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top