Re: Inserting-embedding some html data at the end of a .py file

Discussion in 'Python' started by Íßêïò Ãêñ33ê, Mar 5, 2013.

  1. Thank you very much! This is what i was looking for and here is my code after receiving your help.
    So, with the command you provided to me i can actually run the .py script ans save its output and then append from there!! Great!

    Here is my code now!
    ====================================
    if htmlpage.endswith('.html'):
    f = open( "/home/nikos/public_html/" + htmlpage )
    htmldata = f.read()
    elif htmlpage.endswith('.py'):
    htmldata = subprocess.check_output( open( "/home/nikos/public_html/cgi-bin/" + htmlpage ) )


    counter = ''' <center><a href="mailto:"> <img src="/data/images/mail.png"> </a>
    <center><table border=2 cellpadding=2 bgcolor=black>
    <td><font color=lime>Áñéèìüò Åðéóêåðôþí</td>
    <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
    ''' % data[0]


    template = htmldata + counter
    print ( template )
    =======================================

    But i'am getting this error:
    <type 'exceptions.AttributeError'>: 'module' object has no attribute 'check_output'

    Why does it say it has no attribute?
    Íßêïò Ãêñ33ê, Mar 5, 2013
    #1
    1. Advertising

  2. Íßêïò Ãêñ33ê

    Michael Ross Guest

    On Tue, 05 Mar 2013 23:47:18 +0100, Îίκος ΓκÏ33κ <>
    wrote:

    > Thank you very much! This is what i was looking for and here is my code
    > after receiving your help.
    > So, with the command you provided to me i can actually run the .py
    > script ans save its output and then append from there!! Great!
    >
    > Here is my code now!
    > ====================================
    > if htmlpage.endswith('.html'):
    > f = open( "/home/nikos/public_html/" + htmlpage )
    > htmldata = f.read()
    > elif htmlpage.endswith('.py'):
    > htmldata = subprocess.check_output( open(
    > "/home/nikos/public_html/cgi-bin/" + htmlpage ) )
    >
    >
    > counter = ''' <center><a href="mailto:"> <img
    > src="/data/images/mail.png"> </a>
    > <center><table border=2 cellpadding=2 bgcolor=black>
    > <td><font color=lime>ΑÏιθμός Επισκεπτών</td>
    > <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d
    > </td>
    > ''' % data[0]
    >
    >
    > template = htmldata + counter
    > print ( template )
    > =======================================
    >
    > But i'am getting this error:
    > <type 'exceptions.AttributeError'>: 'module' object has no attribute
    > 'check_output'
    >
    > Why does it say it has no attribute?


    Python version < 2.7 ?

    And it's more along the lines of
    subprocess.check_output( '/home/nikos/.../' + htmlpage )
    without "open".
    Or even
    subprocess.check_output( [ '/your/python/interpreter', '/home/nikos/...'
    ] )
    Michael Ross, Mar 5, 2013
    #2
    1. Advertising

  3. htmldata = subprocess.check_output( '/home/nikos/public_html/cgi-bin/' + htmlpage )

    htmldata = subprocess.check_output( ['/usr/bin/python', '/home/nikos/public_html/cgi-bin/' + htmlpage] )

    Both of the above statemnts fail i'am afraid with the same error message.
    Íßêïò Ãêñ33ê, Mar 5, 2013
    #3
  4. htmldata = subprocess.check_output( '/home/nikos/public_html/cgi-bin/' + htmlpage )

    htmldata = subprocess.check_output( ['/usr/bin/python', '/home/nikos/public_html/cgi-bin/' + htmlpage] )

    Both of the above statemnts fail i'am afraid with the same error message.
    Íßêïò Ãêñ33ê, Mar 5, 2013
    #4
  5. Íßêïò Ãêñ33ê

    Michael Ross Guest

    On Wed, 06 Mar 2013 00:39:31 +0100, Îίκος ΓκÏ33κ <>
    wrote:

    > htmldata = subprocess.check_output( '/home/nikos/public_html/cgi-bin/' +
    > htmlpage )
    >
    > htmldata = subprocess.check_output( ['/usr/bin/python',
    > '/home/nikos/public_html/cgi-bin/' + htmlpage] )
    >
    > Both of the above statemnts fail i'am afraid with the same error message.



    check_output is available as of Python 2.7

    I guess you are still on version 2.6 ?
    Michael Ross, Mar 6, 2013
    #5
  6. Ôç ÔåôÜñôç, 6 Ìáñôßïõ 2013 2:06:33 ð.ì.UTC+2, ï ÷ñÞóôçò Michael Ross Ýãñáøå:
    > On Wed, 06 Mar 2013 00:39:31 +0100, Íßêïò Ãêñ33ê
    >
    > wrote:
    >
    >
    >
    > > htmldata = subprocess.check_output( '/home/nikos/public_html/cgi-bin/' +

    >
    > > htmlpage )

    >
    > >

    >
    > > htmldata = subprocess.check_output( ['/usr/bin/python',

    >
    > > '/home/nikos/public_html/cgi-bin/' + htmlpage] )

    >
    > >

    >
    > > Both of the above statemnts fail i'am afraid with the same error message.


    > check_output is available as of Python 2.7
    > I guess you are still on version 2.6 ?


    Actually i'am runnign myu cgi script on my hostgator remote web hosting.
    How dod i check the version of python the server uses?
    Íßêïò Ãêñ33ê, Mar 6, 2013
    #6
  7. Ôç ÔåôÜñôç, 6 Ìáñôßïõ 2013 2:06:33 ð.ì.UTC+2, ï ÷ñÞóôçò Michael Ross Ýãñáøå:
    > On Wed, 06 Mar 2013 00:39:31 +0100, Íßêïò Ãêñ33ê
    >
    > wrote:
    >
    >
    >
    > > htmldata = subprocess.check_output( '/home/nikos/public_html/cgi-bin/' +

    >
    > > htmlpage )

    >
    > >

    >
    > > htmldata = subprocess.check_output( ['/usr/bin/python',

    >
    > > '/home/nikos/public_html/cgi-bin/' + htmlpage] )

    >
    > >

    >
    > > Both of the above statemnts fail i'am afraid with the same error message.


    > check_output is available as of Python 2.7
    > I guess you are still on version 2.6 ?


    Actually i'am runnign myu cgi script on my hostgator remote web hosting.
    How dod i check the version of python the server uses?
    Íßêïò Ãêñ33ê, Mar 6, 2013
    #7
  8. Ôç ÔåôÜñôç, 6 Ìáñôßïõ 2013 2:06:33 ð.ì.UTC+2, ï ÷ñÞóôçò Michael Ross Ýãñáøå:

    > check_output is available as of Python 2.7
    >
    >
    >
    > I guess you are still on version 2.6 ?


    Indeed!
    i just checked it's Python 2.6.6: /usr/bin/python and i have asked the linxu admins @ hostgator.com to update python.

    I trust when they update to v2.7 the script will automatically work!
    Íßêïò Ãêñ33ê, Mar 6, 2013
    #8
  9. Ôç ÔåôÜñôç, 6 Ìáñôßïõ 2013 2:06:33 ð.ì.UTC+2, ï ÷ñÞóôçò Michael Ross Ýãñáøå:

    > check_output is available as of Python 2.7
    >
    >
    >
    > I guess you are still on version 2.6 ?


    Indeed!
    i just checked it's Python 2.6.6: /usr/bin/python and i have asked the linxu admins @ hostgator.com to update python.

    I trust when they update to v2.7 the script will automatically work!
    Íßêïò Ãêñ33ê, Mar 6, 2013
    #9
  10. Ôç ÔåôÜñôç, 6 Ìáñôßïõ 2013 2:06:33 ð.ì.UTC+2, ï ÷ñÞóôçò Michael Ross Ýãñáøå:

    > check_output is available as of Python 2.7
    > I guess you are still on version 2.6 ?



    I can access each of these from my jailed shell user account without issue,and especially i try /usr/bin/python3

    [~]# /usr/bin/python -V
    Python 2.6.6

    [~]# /opt/python3/bin/python3 -V
    Python 3.2.3

    [~]# /usr/bin/python3 -V
    Python 3.2.3


    Problem is that when i change my shebang constructor to #!/sur/bin/python3 my '/cgi-bin/metrites.py' python cgi scripts produces an internal server error while with #!/sur/bin/python the script works.

    Any ideas as to why? I can post ocde if you want to.
    Íßêïò Ãêñ33ê, Mar 6, 2013
    #10
  11. Ôç ÔåôÜñôç, 6 Ìáñôßïõ 2013 2:06:33 ð.ì.UTC+2, ï ÷ñÞóôçò Michael Ross Ýãñáøå:

    > check_output is available as of Python 2.7
    > I guess you are still on version 2.6 ?



    I can access each of these from my jailed shell user account without issue,and especially i try /usr/bin/python3

    [~]# /usr/bin/python -V
    Python 2.6.6

    [~]# /opt/python3/bin/python3 -V
    Python 3.2.3

    [~]# /usr/bin/python3 -V
    Python 3.2.3


    Problem is that when i change my shebang constructor to #!/sur/bin/python3 my '/cgi-bin/metrites.py' python cgi scripts produces an internal server error while with #!/sur/bin/python the script works.

    Any ideas as to why? I can post ocde if you want to.
    Íßêïò Ãêñ33ê, Mar 6, 2013
    #11
  12. Íßêïò Ãêñ33ê

    Michael Ross Guest

    On Thu, 07 Mar 2013 00:18:44 +0100, Îίκος ΓκÏ33κ <>
    wrote:

    > Τη ΤετάÏτη, 6 ΜαÏτίου 2013 2:06:33 Ï€.μ. UTC+2, οχÏήστης Michael Ross
    > έγÏαψε:
    >
    >> check_output is available as of Python 2.7
    >> I guess you are still on version 2.6 ?

    >
    >
    > I can access each of these from my jailed shell user account without
    > issue, and especially i try /usr/bin/python3
    >
    > [~]# /usr/bin/python -V
    > Python 2.6.6
    >
    > [~]# /opt/python3/bin/python3 -V
    > Python 3.2.3
    >
    > [~]# /usr/bin/python3 -V
    > Python 3.2.3
    >
    >
    > Problem is that when i change my shebang constructor to
    > #!/sur/bin/python3 my '/cgi-bin/metrites.py' python cgi scripts produces
    > an internal server error while with #!/sur/bin/python the script works..
    >
    > Any ideas as to why? I can post ocde if you want to.



    You need to post the Traceback.

    Either run /usr/bin/python3 /cgi-bin/metrites.py on the shell
    or better look in your webserver error log.

    Guess:
    In Python 3 "print" is a function.
    So
    print "something"
    will not work. You need to
    print("something")
    Michael Ross, Mar 7, 2013
    #12
  13. Ôç ÐÝìðôç, 7 Ìáñôßïõ 2013 2:25:09 ð.ì. UTC+2, ï ÷ñÞóôçò Michael Ross Ýãñáøå:

    > Either run /usr/bin/python3 /cgi-bin/metrites.py on the shell
    > or better look in your webserver error log.


    > Guess:
    > In Python 3 "print" is a function.
    > So
    > print "something"
    > will not work. You need to
    >
    > print("something")


    Yes Michael i have already prinr with parenthesis except form the triple quoting:


    print ''' <center><h2><font color=lime> LOG </font> üëùí ôùí <font color=red> HTML </font> óåëßäùí</h2><br>
    <table border=5 cellpadding=5 bgcolor=black>
    <th><font color=orange size=5> Óåëßäá </th> <th><font color=orange size=5> ÅðéóêÝøåéò </th>
    '''

    have turned to:


    print(''' <center><h2><font color=lime> LOG </font> üëùí ôùí <font color=red> HTML </font> óåëßäùí</h2><br>
    <table border=5 cellpadding=5 bgcolor=black>
    <th><font color=orange size=5> Óåëßäá </th> <th><font color=orange size=5> ÅðéóêÝøåéò </th>
    ''')


    but now iam receiving this error concering except:

    [~/www/cgi-bin]# /usr/bin/python3 metrites.py
    File "metrites.py", line 88
    except MySQLdb.Error, e:
    ^
    SyntaxError: invalid syntax
    [~/www/cgi-bin]#

    which used to work ok in v2.6.6

    can you help?
    Íßêïò Ãêñ33ê, Mar 7, 2013
    #13
  14. Ôç ÐÝìðôç, 7 Ìáñôßïõ 2013 2:25:09 ð.ì. UTC+2, ï ÷ñÞóôçò Michael Ross Ýãñáøå:

    > Either run /usr/bin/python3 /cgi-bin/metrites.py on the shell
    > or better look in your webserver error log.


    > Guess:
    > In Python 3 "print" is a function.
    > So
    > print "something"
    > will not work. You need to
    >
    > print("something")


    Yes Michael i have already prinr with parenthesis except form the triple quoting:


    print ''' <center><h2><font color=lime> LOG </font> üëùí ôùí <font color=red> HTML </font> óåëßäùí</h2><br>
    <table border=5 cellpadding=5 bgcolor=black>
    <th><font color=orange size=5> Óåëßäá </th> <th><font color=orange size=5> ÅðéóêÝøåéò </th>
    '''

    have turned to:


    print(''' <center><h2><font color=lime> LOG </font> üëùí ôùí <font color=red> HTML </font> óåëßäùí</h2><br>
    <table border=5 cellpadding=5 bgcolor=black>
    <th><font color=orange size=5> Óåëßäá </th> <th><font color=orange size=5> ÅðéóêÝøåéò </th>
    ''')


    but now iam receiving this error concering except:

    [~/www/cgi-bin]# /usr/bin/python3 metrites.py
    File "metrites.py", line 88
    except MySQLdb.Error, e:
    ^
    SyntaxError: invalid syntax
    [~/www/cgi-bin]#

    which used to work ok in v2.6.6

    can you help?
    Íßêïò Ãêñ33ê, Mar 7, 2013
    #14
  15. The whole try stement is as follows to have the compete idea:

    try:
    cur.execute( '''SELECT url, hits FROM counters ORDER BY hits DESC''' )

    data = cur.fetchall()
    for row in data:
    (url, hits) = row

    print( "<tr><td><center><a href='http://superhost.gr/?show=log&htmlpage=%s'><font color=tomato size=5> %s </a></td>" ) % (url, url)
    print( "<td><center><font color=cyan size=5> %s </a></td></tr>" ) % (hits)
    except MySQLdb.Error, e:
    print( "Query Error: ", sys.exc_info()[1].excepinfo()[2] )
    Íßêïò Ãêñ33ê, Mar 7, 2013
    #15
  16. The whole try stement is as follows to have the compete idea:

    try:
    cur.execute( '''SELECT url, hits FROM counters ORDER BY hits DESC''' )

    data = cur.fetchall()
    for row in data:
    (url, hits) = row

    print( "<tr><td><center><a href='http://superhost.gr/?show=log&htmlpage=%s'><font color=tomato size=5> %s </a></td>" ) % (url, url)
    print( "<td><center><font color=cyan size=5> %s </a></td></tr>" ) % (hits)
    except MySQLdb.Error, e:
    print( "Query Error: ", sys.exc_info()[1].excepinfo()[2] )
    Íßêïò Ãêñ33ê, Mar 7, 2013
    #16
  17. Hi,

    > but now iam receiving this error concering except:
    >
    > [~/www/cgi-bin]# /usr/bin/python3 metrites.py
    > File "metrites.py", line 88
    > except MySQLdb.Error, e:
    > ^
    > SyntaxError: invalid syntax
    > [~/www/cgi-bin]#
    >
    > which used to work ok in v2.6.6
    >
    > can you help?

    Python 3 introduced some Python 2-incompatible changes.
    So, please read: http://docs.python.org/release/3.0.1/whatsnew/3.0.html

    Roland
    Roland Koebler, Mar 7, 2013
    #17
  18. Ôç ÐÝìðôç, 7 Ìáñôßïõ 2013 10:58:13 ð.ì. UTC+2, ï ÷ñÞóôçò Roland Koebler Ýãñáøå:
    > Hi,
    >
    >
    >
    > > but now iam receiving this error concering except:

    >
    > >

    >
    > > [~/www/cgi-bin]# /usr/bin/python3 metrites.py

    >
    > > File "metrites.py", line 88

    >
    > > except MySQLdb.Error, e:

    >
    > > ^

    >
    > > SyntaxError: invalid syntax

    >
    > > [~/www/cgi-bin]#

    >
    > >

    >
    > > which used to work ok in v2.6.6

    >
    > >

    >
    > > can you help?

    >
    > Python 3 introduced some Python 2-incompatible changes.
    >
    > So, please read: http://docs.python.org/release/3.0.1/whatsnew/3.0.html
    >
    >
    >
    > Roland


    I did read about print but i'm interesetd to see and its not mentioned there how

    except MySQLdb.Error, e:
    print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] )

    is supposed to be written as in Python v3.x
    Íßêïò Ãêñ33ê, Mar 7, 2013
    #18
  19. Ôç ÐÝìðôç, 7 Ìáñôßïõ 2013 10:58:13 ð.ì. UTC+2, ï ÷ñÞóôçò Roland Koebler Ýãñáøå:
    > Hi,
    >
    >
    >
    > > but now iam receiving this error concering except:

    >
    > >

    >
    > > [~/www/cgi-bin]# /usr/bin/python3 metrites.py

    >
    > > File "metrites.py", line 88

    >
    > > except MySQLdb.Error, e:

    >
    > > ^

    >
    > > SyntaxError: invalid syntax

    >
    > > [~/www/cgi-bin]#

    >
    > >

    >
    > > which used to work ok in v2.6.6

    >
    > >

    >
    > > can you help?

    >
    > Python 3 introduced some Python 2-incompatible changes.
    >
    > So, please read: http://docs.python.org/release/3.0.1/whatsnew/3.0.html
    >
    >
    >
    > Roland


    I did read about print but i'm interesetd to see and its not mentioned there how

    except MySQLdb.Error, e:
    print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] )

    is supposed to be written as in Python v3.x
    Íßêïò Ãêñ33ê, Mar 7, 2013
    #19
  20. Íßêïò Ãêñ33ê

    Lele Gaifax Guest

    Îίκος ΓκÏ33κ <> writes:

    > I did read about print but i'm interesetd to see and its not mentioned there how
    >
    > except MySQLdb.Error, e:
    > print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] )
    >
    > is supposed to be written as in Python v3.x


    Read better:
    http://docs.python.org/release/3.0.1/whatsnew/3.0.html#changed-syntax
    4th (bullet) point.

    ciao, lele.
    --
    nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
    real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
    | -- Fortunato Depero, 1929.
    Lele Gaifax, Mar 7, 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. Jimmy
    Replies:
    3
    Views:
    327
    J. J. Farrell
    Sep 9, 2011
  2. Renie83
    Replies:
    1
    Views:
    175
    Kiran Math
    Jul 10, 2003
  3. Íßêïò Ãêñ33ê
    Replies:
    2
    Views:
    142
    Joel Goldstick
    Mar 5, 2013
  4. Íßêïò Ãêñ33ê
    Replies:
    9
    Views:
    137
    Roland Koebler
    Mar 6, 2013
  5. Íßêïò Ãêñ33ê
    Replies:
    4
    Views:
    150
    Íßêïò Ãêñ33ê
    Mar 5, 2013
Loading...

Share This Page