Forcing Python to detect DocumentRoot

Discussion in 'Python' started by Ferrous Cranus, Jan 16, 2013.

  1. 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?
     
    Ferrous Cranus, Jan 16, 2013
    #1
    1. Advertising

  2. On Wed, Jan 16, 2013 at 8:51 AM, Ferrous Cranus <>wrote:

    > 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: http://docs.python.org/2/library/os.path.html

    --
    Joel Goldstick
    http://joelgoldstick.com
     
    Joel Goldstick, Jan 16, 2013
    #2
    1. Advertising

  3. Τη ΤετάÏτη, 16 ΙανουαÏίου 2013 4:01:07 μ.μ. UTC+2, ο χÏήστης Joel Goldstick έγÏαψε:
    > On Wed, Jan 16, 2013 at 8:51 AM, Ferrous Cranus <> wrote:
    >
    > 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.
     
    Ferrous Cranus, Jan 16, 2013
    #3
  4. 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?
     
    Ferrous Cranus, Jan 17, 2013
    #4
  5. Ferrous Cranus

    rusi Guest

    On Jan 16, 6:51 pm, Ferrous Cranus <> wrote:
    > 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()
     
    rusi, Jan 17, 2013
    #5
  6. Ferrous Cranus

    Roy Smith Guest

    In article <>,
    Ferrous Cranus <> wrote:

    > 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?
     
    Roy Smith, Jan 17, 2013
    #6
  7. On Thu, Jan 17, 2013 at 9:09 AM, Roy Smith <> wrote:

    > In article <>,
    > Ferrous Cranus <> wrote:
    >
    > > 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 .



    --
    Joel Goldstick
    http://joelgoldstick.com
     
    Joel Goldstick, Jan 17, 2013
    #7
  8. Τη Πέμπτη, 17 ΙανουαÏίου 2013 5:14:19 μ.μ. UTC+2, ο χÏήστης Joel Goldstick έγÏαψε:
    > On Thu, Jan 17, 2013 at 9:09 AM, Roy Smith <> wrote:
    >
    > In article <>,
    >
    >
    >
    >  Ferrous Cranus <> wrote:
    >
    >
    >
    > > 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...
     
    Ferrous Cranus, Jan 18, 2013
    #8
  9. Τη Πέμπτη, 17 ΙανουαÏίου 2013 5:14:19 μ.μ. UTC+2, ο χÏήστης Joel Goldstick έγÏαψε:
    > On Thu, Jan 17, 2013 at 9:09 AM, Roy Smith <> wrote:
    >
    > In article <>,
    >
    >
    >
    >  Ferrous Cranus <> wrote:
    >
    >
    >
    > > 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...
     
    Ferrous Cranus, Jan 18, 2013
    #9
  10. On Fri, Jan 18, 2013 at 8:02 AM, Ferrous Cranus <>wrote:

    > Τη Πέμπτη, 17 ΙανουαÏίου 2013 5:14:19 μ.μ. UTC+2, ο χÏήστης Joel Goldstick
    > έγÏαψε:
    > > On Thu, Jan 17, 2013 at 9:09 AM, Roy Smith <> wrote:
    > >
    > > In article <>,
    > >
    > >
    > >
    > > Ferrous Cranus <> wrote:
    > >
    > >
    > >
    > > > 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...
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >



    DocumentRoot = os.environ['HOME'] + 'public_html'
    --
    Joel Goldstick
    http://joelgoldstick.com
     
    Joel Goldstick, Jan 18, 2013
    #10
  11. On Friday, January 18, 2013, Ferrous Cranus wrote:

    > Ôç ÐÝìðôç, 17 Éáíïõáñßïõ 2013 5:14:19ì.ì. UTC+2, ï ÷ñÞóôçò Joel Goldstick
    > Ýãñáøå:
    > > On Thu, Jan 17, 2013 at 9:09 AM, Roy Smith <<javascript:;>>

    > wrote:
    > >
    > > In article <<javascript:;>
    > >,
    > >
    > >
    > >
    > > Ferrous Cranus < <javascript:;>> wrote:
    > >
    > >
    > >
    > > > 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?

    > >



    $ 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...
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Rodrick Brown, Jan 18, 2013
    #11
  12. Τη ΠαÏασκευή, 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?
     
    Ferrous Cranus, Jan 18, 2013
    #12
  13. Τη ΠαÏασκευή, 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?
     
    Ferrous Cranus, Jan 18, 2013
    #13
  14. On Fri, Jan 18, 2013 at 1:58 PM, Ferrous Cranus <>wrote:

    > Τη ΠαÏασκευή, 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?
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >


    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

    --
    Joel Goldstick
    http://joelgoldstick.com
     
    Joel Goldstick, Jan 18, 2013
    #14
  15. 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?
     
    Ferrous Cranus, Jan 18, 2013
    #15
  16. 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?
     
    Ferrous Cranus, Jan 18, 2013
    #16
  17. On Sat, Jan 19, 2013 at 5:58 AM, Ferrous Cranus <> wrote:
    > Ôç ÐáñáóêåõÞ, 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
     
    Chris Angelico, Jan 18, 2013
    #17
  18. Τη ΠαÏασκευή, 18 ΙανουαÏίου 2013 11:34:05 μ.μ. UTC+2, ο χÏήστης Chris Angelico έγÏαψε:
    > On Sat, Jan 19, 2013 at 5:58 AM, Ferrous Cranus <> wrote:
    >
    > > Τη ΠαÏασκευή, 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:"> <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?
     
    Ferrous Cranus, Jan 19, 2013
    #18
  19. Τη ΠαÏασκευή, 18 ΙανουαÏίου 2013 11:34:05 μ.μ. UTC+2, ο χÏήστης Chris Angelico έγÏαψε:
    > On Sat, Jan 19, 2013 at 5:58 AM, Ferrous Cranus <> wrote:
    >
    > > Τη ΠαÏασκευή, 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:"> <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?
     
    Ferrous Cranus, Jan 19, 2013
    #19
  20. Ferrous Cranus

    Barry Scott Guest

    On 16 Jan 2013, at 13:51, Ferrous Cranus <> wrote:

    > 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
     
    Barry Scott, Jan 19, 2013
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mdb
    Replies:
    3
    Views:
    4,225
    =?Utf-8?B?TmVpbA==?=
    Sep 20, 2004
  2. DonQ
    Replies:
    2
    Views:
    936
    Amontec Team, Laurent Gauch
    Feb 28, 2004
  3. Jluis
    Replies:
    7
    Views:
    531
    Just an Illusion
    Jun 11, 2004
  4. Matt North

    'X' - Forcing Unknown

    Matt North, Nov 16, 2004, in forum: VHDL
    Replies:
    2
    Views:
    578
  5. buffi
    Replies:
    14
    Views:
    438
    James Stroud
    Sep 17, 2007
Loading...

Share This Page