Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

68 views
Skip to first unread message

Νίκος Γκρ33κ

unread,
Mar 5, 2013, 9:35:59 AM3/5/13
to
# ==================================================================================================== =============
# return general counter, render the page and print it
# ==================================================================================================== =============
cur.execute( '''SELECT hits FROM counters WHERE URL = %s''', (htmlpage,) )
data = cur.fetchone()

if cur.rowcount:
# open html template
if htmlpage.endswith('.html'):
f = open( "/home/nikos/public_html/" + htmlpage )
else:
f = open( "/home/nikos/public_html/cgi-bin/" + htmlpage )


# render html template and save
htmldata = f.read()
counter = '''<center><a href="mailto:sup...@superhost.gr"> <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 )
===============================

I'am trying to emven a counter after each .html or .py file thet the user requests, so to keep track how many times the file has been accessed.
The html files appear and work ok, but the .py files appear as documents instead of runnign as cgi scripts.

Can you help please?

Νίκος Γκρ33κ

unread,
Mar 5, 2013, 12:11:07 PM3/5/13
to
Any ideas on hopw to embed some html data at the end of each python script?

Mark Lawrence

unread,
Mar 5, 2013, 12:19:53 PM3/5/13
to pytho...@python.org
On 05/03/2013 17:11, Νίκος Γκρ33κ wrote:
> Any ideas on hopw to embed some html data at the end of each python script?
>

Write some code.

--
Cheers.

Mark Lawrence

Joel Goldstick

unread,
Mar 5, 2013, 12:25:54 PM3/5/13
to Mark Lawrence, pytho...@python.org
On Tue, Mar 5, 2013 at 12:19 PM, Mark Lawrence <bream...@yahoo.co.uk> wrote:
On 05/03/2013 17:11, Νίκος Γκρ33κ wrote:
Any ideas on hopw to embed some html data at the end of each python script?

Νίκος Γκρ33κ

unread,
Mar 5, 2013, 12:39:19 PM3/5/13
to
But i did, I just tried this:

# open html template
if htmlpage.endswith('.html'):
f = open( "/home/nikos/public_html/" + htmlpage )

htmldata = f.read()
counter = ''' <center><a href="mailto:sup...@superhost.gr"> <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]
else:
f = open( "/home/nikos/public_html/cgi-bin/" + htmlpage )

htmldata = f.read()
counter = '''
print '''<center><a href="mailto:sup...@superhost.gr"> <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 still doens't embed correctly the additional html data at the end of the .py files.....

Do you have an idea?

Joel Goldstick

unread,
Mar 5, 2013, 12:45:25 PM3/5/13
to Νίκος Γκρ33κ, pytho...@python.org
On Tue, Mar 5, 2013 at 12:39 PM, Νίκος Γκρ33κ <nikos...@gmail.com> wrote:
But i did, I just tried this:

        # open html template
        if htmlpage.endswith('.html'):
                f = open( "/home/nikos/public_html/" + htmlpage )

                htmldata = f.read()
                counter =       ''' <center><a href="mailto:sup...@superhost.gr"> <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]
        else:
                f = open( "/home/nikos/public_html/cgi-bin/" + htmlpage )

                htmldata = f.read()
                counter =       '''
                                        print '''<center><a href="mailto:sup...@superhost.gr"> <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>
                                        '''

remove the extra triple quote.  But seriously, in 2013 you are writing html like the above?  That is some awful stuff
                                        ''' % data[0]

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

But still doens't embed correctly the additional html data at the end of the .py files.....

Do you have an idea?

Νίκος Γκρ33κ

unread,
Mar 5, 2013, 12:49:28 PM3/5/13
to Νίκος Γκρ33κ, pytho...@python.org
What extra triple quote?
There are 2 sets of triple quotes the counter's and the print's !!

in case htmlpage variable is a .py file i must append a print '''html''' in order for .py code to absorve that code properly no?

Mark Lawrence

unread,
Mar 5, 2013, 12:53:10 PM3/5/13
to pytho...@python.org
You're doing something wrong.

--
Cheers.

Mark Lawrence

Νίκος Γκρ33κ

unread,
Mar 5, 2013, 12:49:28 PM3/5/13
to comp.lan...@googlegroups.com, Νίκος Γκρ33κ, pytho...@python.org

Dave Angel

unread,
Mar 5, 2013, 1:18:16 PM3/5/13
to pytho...@python.org
On 03/05/2013 12:49 PM, Νίκος Γκρ33κ wrote:
> What extra triple quote?
> There are 2 sets of triple quotes the counter's and the print's !!

There are 3 pairs of triple-quotes. But one pair is nested inside the
other, so the interpreter will not handle it the way you apparently
want. If you have to use quotes inside quotes, you must either use a
different kind, or escape them. Perhaps you wanted something like:

counter = """
print '''<center> etc....
<td>font ...
'''
""" % data[0]

But this fragment is unreadable to me, so the syntax error is probably
the least of your worries.

>
> in case htmlpage variable is a .py file i must append a print '''html''' in order for .py code to absorve that code properly no?
>

No clue what that sentence fragment means.

'absorve' isn't an English word, and neither 'absorb' nor 'observe' make
any sense.

Perhaps if you posted a complete program, and identified it by name, and
specified the complete environment it runs in, and used English to
describe what should be appended to what, we'd be able to help. As it
is, you've got mixed clues from which that it's both Python 2 and 3,
running Linux or equivalent, and probably runs on a server.

Using symbol names that make sense would also help. 'htmlpage' is
apparently a str representing a filename on disk. 'counter' is not an
integer, but a long string of html. 'data' is apparently a tuple or
list, and the zeroth element is an int. ??

--
--
DaveA

Νίκος Γκρ33κ

unread,
Mar 5, 2013, 1:53:25 PM3/5/13
to pytho...@python.org
Let's focus on just the following snipper please:

f = open( some_python_file )

htmldata = f.read()
counter = ''' print( "
<center><a href="mailto:sup...@superhost.gr"> <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]

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

What this snippet tries to accomplish is append the following string

=========================================
counter = ''' <center><a href="mailto:sup...@superhost.gr"> <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
=========================================

at the end of the the python script file that it currently opened.
I'am using the print statemnt inside the triple quoted string so to append that html data by inserting a print statemnt but although i have changes the type of quoting it still fails.

If i try to append that html data to an .html file they are inserted beautifully but inside a .py file they dont.

I just need to append that string after the end of a .py file. that's all i want to do.

Joel Goldstick

unread,
Mar 5, 2013, 2:33:59 PM3/5/13
to Νίκος Γκρ33κ, pytho...@python.org
print displays to the console.  To write to a file open file in 'append' mode and write:

with open("test.py", "a") as myfile:
    myfile.write("appended text")

Νίκος Γκρ33κ

unread,
Mar 5, 2013, 1:53:25 PM3/5/13
to comp.lan...@googlegroups.com, pytho...@python.org
Let's focus on just the following snipper please:

f = open( some_python_file )

htmldata = f.read()
counter = ''' print( "
<center><a href="mailto:sup...@superhost.gr"> <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]

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

What this snippet tries to accomplish is append the following string

=========================================
counter = ''' <center><a href="mailto:sup...@superhost.gr"> <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
=========================================

Dave Angel

unread,
Mar 5, 2013, 2:48:45 PM3/5/13
to pytho...@python.org
On 03/05/2013 01:53 PM, οΏ½οΏ½οΏ½οΏ½οΏ½ οΏ½οΏ½οΏ½33οΏ½ wrote:
> Let's focus on just the following snipper please:

Once again, I repeat. Make a fragment that contains enough information
to actually run. Explain in what environment it's running, and what you
hoped would happen. For example, why on earth would you assume that
printing to the console below would write to a file instead? Is some
part of this running as a cgi inside a web server? What is your
environment?
>
> f = open( some_python_file )
>
> htmldata = f.read()
> counter = ''' print( "

Syntax error here. Since the inner string is more than one line, you
will need to use """. See my example last message. This only matters
if you succeed in writing this to a python file.

> <center><a href="mailto:sup...@superhost.gr"> <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]
>
> #render template
> template = htmldata + counter
> print ( template )

Why should print write to some python file you have open?? Besides you
have the file open for readonly access. So you might use:

f.close()
f = open( some_python_file, "w" )
f.write(template)
f.close()


> =============================
>
> What this snippet tries to accomplish is append the following string
>
> =========================================
> counter = ''' <center><a href="mailto:sup...@superhost.gr"> <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
> =========================================
>
> at the end of the the python script file that it currently opened.

But that's not at all what's inside the counter variable.

> I'am using the print statemnt inside the triple quoted string so to append that html data by inserting a print statemnt but although i have changes the type of quoting it still fails.

Don't ever say "it fails". If you can't be bothered to explain in what
way it fails, forget it. Perhaps you're trying to say it prints a
string to the console instead of writing a different string to the
/fullpath/to/some_python_file.py

Or perhaps you're saying it crashed the Windows machine, and killed
power for miles around. Or perhaps you're saying it got an exception
but we won't bother telling you which.


>
> If i try to append that html data to an .html file they are inserted beautifully

Untrue, unless your environment is specified, it certainly did not.
perhaps you were running the program with output redirected to a file,
in which case it lost the earlier version. Or perhaps you were running
it on a web server, and you *think* some file changed because some
obscure thing happened on your browser. What's your environment?

> but inside a .py file they dont.
>
> I just need to append that string after the end of a .py file. that's all i want to do.
>

Easy to do in emacs.


--
DaveA

Νίκος Γκρ33κ

unread,
Mar 5, 2013, 3:04:59 PM3/5/13
to
#open html template
if htmlpage.endswith('.html'):
f = open( "/home/nikos/public_html/" + htmlpage )

htmldata = f.read()
counter = ''' <center><a href="mailto:sup...@superhost.gr"> <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]

#render template
template = htmldata + counter
print ( template )
============================================
Yes the aboev code does work if we talk about appending html data to an already html file!

But if the file is some_python.py file then i cannot just append the data.
Actually by appending i dont want to actually insert the data to the end of the .py file, thus altering it but instead run the .py file and print the counter html data afterwards!

Dave Angel

unread,
Mar 5, 2013, 4:02:18 PM3/5/13
to pytho...@python.org
In other words, you're just trying to waste our time. Thanks for
telling us.

--
DaveA

Michael Ross

unread,
Mar 5, 2013, 4:29:41 PM3/5/13
to pytho...@python.org
On Tue, 05 Mar 2013 21:04:59 +0100, Νίκος Γκρ33κ <nikos...@gmail.com>
wrote:
subprocess.checkoutput() ?

if htmlpage.endswith('.py'):
htmldata=subprocess.check_output(...)
counter=...
template=htmldata+counter

http://docs.python.org/2/library/subprocess.html?highlight=check_output#subprocess.check_output




Νίκος Γκρ33κ

unread,
Mar 5, 2013, 5:47:18 PM3/5/13
to
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:sup...@superhost.gr"> <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κ

unread,
Mar 5, 2013, 5:49:49 PM3/5/13
to pytho...@python.org
Τη Τρίτη, 5 Μαρτίου 2013 11:02:18 μ.μ. UTC+2, ο χρήστης Dave Angel έγραψε:

> In other words, you're just trying to waste our time. Thanks for
>
> telling us.

Honestly, its not in my intentions to waste your time.
I appreciate all the great help you have provided me in all of my questions, i really do.

I'am sorry if i sometimes cannot express myself correctly in english, hence i cannot descrive exactly what i want to accomplish.

I'am sorry about that but english its not my native language.

Νίκος Γκρ33κ

unread,
Mar 5, 2013, 5:49:49 PM3/5/13
to comp.lan...@googlegroups.com, pytho...@python.org
Τη Τρίτη, 5 Μαρτίου 2013 11:02:18 μ.μ. UTC+2, ο χρήστης Dave Angel έγραψε:

> In other words, you're just trying to waste our time. Thanks for
>
> telling us.

Michael Ross

unread,
Mar 5, 2013, 6:19:03 PM3/5/13
to pytho...@python.org
On Tue, 05 Mar 2013 23:47:18 +0100, Νίκος Γκρ33κ <nikos...@gmail.com>
wrote:
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/...'
] )

Νίκος Γκρ33κ

unread,
Mar 5, 2013, 6:39:31 PM3/5/13
to pytho...@python.org
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κ

unread,
Mar 5, 2013, 6:39:31 PM3/5/13
to comp.lan...@googlegroups.com, pytho...@python.org

Michael Ross

unread,
Mar 5, 2013, 7:06:33 PM3/5/13
to pytho...@python.org
On Wed, 06 Mar 2013 00:39:31 +0100, Νίκος Γκρ33κ <nikos...@gmail.com>
wrote:
check_output is available as of Python 2.7

I guess you are still on version 2.6 ?


Νίκος Γκρ33κ

unread,
Mar 6, 2013, 1:12:01 AM3/6/13
to pytho...@python.org
Τη Τετάρτη, 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κ

unread,
Mar 6, 2013, 1:12:01 AM3/6/13
to comp.lan...@googlegroups.com, pytho...@python.org
Τη Τετάρτη, 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 ?

Νίκος Γκρ33κ

unread,
Mar 6, 2013, 2:48:18 AM3/6/13
to pytho...@python.org
Τη Τετάρτη, 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κ

unread,
Mar 6, 2013, 2:48:18 AM3/6/13
to comp.lan...@googlegroups.com, pytho...@python.org
Τη Τετάρτη, 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 ?

Roland Koebler

unread,
Mar 6, 2013, 4:00:14 AM3/6/13
to pytho...@python.org
Hi,
as someone said: You're doing it the wrong way.

I would recommend to use a template-engine; then you can put the
complete html-design (and some design-control-structures) into
the template (and *not* into the cgi) and fill data into the
template with a python-script.


Roland

Νίκος Γκρ33κ

unread,
Mar 6, 2013, 6:18:44 PM3/6/13
to pytho...@python.org
Τη Τετάρτη, 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

ni...@superhost.gr [~]# /usr/bin/python -V
Python 2.6.6

ni...@superhost.gr [~]# /opt/python3/bin/python3 -V
Python 3.2.3

ni...@superhost.gr [~]# /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κ

unread,
Mar 6, 2013, 6:18:44 PM3/6/13
to comp.lan...@googlegroups.com, pytho...@python.org
Τη Τετάρτη, 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 ?


Michael Ross

unread,
Mar 6, 2013, 7:25:09 PM3/6/13
to pytho...@python.org
On Thu, 07 Mar 2013 00:18:44 +0100, Νίκος Γκρ33κ <nikos...@gmail.com>
wrote:
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")

Νίκος Γκρ33κ

unread,
Mar 7, 2013, 1:35:12 AM3/7/13
to pytho...@python.org
Τη Πέμπτη, 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:

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

which used to work ok in v2.6.6

can you help?

Νίκος Γκρ33κ

unread,
Mar 7, 2013, 1:35:12 AM3/7/13
to comp.lan...@googlegroups.com, pytho...@python.org
Τη Πέμπτη, 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")

Νίκος Γκρ33κ

unread,
Mar 7, 2013, 1:42:05 AM3/7/13
to pytho...@python.org
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κ

unread,
Mar 7, 2013, 1:42:05 AM3/7/13
to comp.lan...@googlegroups.com, pytho...@python.org

Roland Koebler

unread,
Mar 7, 2013, 3:58:13 AM3/7/13
to pytho...@python.org
Hi,

> but now iam receiving this error concering except:
>
> ni...@superhost.gr [~/www/cgi-bin]# /usr/bin/python3 metrites.py
> File "metrites.py", line 88
> except MySQLdb.Error, e:
> ^
> SyntaxError: invalid syntax
> ni...@superhost.gr [~/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

Νίκος Γκρ33κ

unread,
Mar 7, 2013, 4:05:45 AM3/7/13
to pytho...@python.org
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κ

unread,
Mar 7, 2013, 4:05:45 AM3/7/13
to comp.lan...@googlegroups.com, pytho...@python.org
Τη Πέμπτη, 7 Μαρτίου 2013 10:58:13 π.μ. UTC+2, ο χρήστης Roland Koebler έγραψε:

Lele Gaifax

unread,
Mar 7, 2013, 4:22:09 AM3/7/13
to pytho...@python.org
Νίκος Γκρ33κ <nikos...@gmail.com> 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.
le...@metapensiero.it | -- Fortunato Depero, 1929.

Νίκος Γκρ33κ

unread,
Mar 7, 2013, 4:28:51 AM3/7/13
to pytho...@python.org
please give me the exact link to read, i cant seem to find it although you send 4th bullet i do not see it....

Νίκος Γκρ33κ

unread,
Mar 7, 2013, 4:28:51 AM3/7/13
to comp.lan...@googlegroups.com, pytho...@python.org

Lele Gaifax

unread,
Mar 7, 2013, 4:50:42 AM3/7/13
to pytho...@python.org
Νίκος Γκρ33κ <nikos...@gmail.com> writes:

> Τη Πέμπτη, 7 Μαρτίου 2013 11:22:09 π.μ. UTC+2, ο χρήστης Lele Gaifax έγραψε:
>> http://docs.python.org/release/3.0.1/whatsnew/3.0.html#changed-syntax
>>
>> 4th (bullet) point.
>
> please give me the exact link to read, i cant seem to find it although
> you send 4th bullet i do not see it....

Oh come on Νίκος, the above *is* the *exact* link to read, I even
pointed at the right subparagraph: it contains both a link to the
reference manual for the "except" keyword, and a link to the PEP that
talks about the change.

Νίκος Γκρ33κ

unread,
Mar 7, 2013, 4:55:57 AM3/7/13
to pytho...@python.org
call me an idiot but is till dont see it:

i;am currently trying this:

except MySQLdb.OperationalError, e:

but give sme an internal server error too :(

Νίκος Γκρ33κ

unread,
Mar 7, 2013, 4:55:57 AM3/7/13
to comp.lan...@googlegroups.com, pytho...@python.org

Νίκος Γκρ33κ

unread,
Mar 7, 2013, 5:09:59 AM3/7/13
to pytho...@python.org
This fails also:

except MySQLdb.ProgrammingError, e:

How on earth the exception is suppsoed to be written!!!!

Chris Angelico

unread,
Mar 7, 2013, 5:19:05 AM3/7/13
to pytho...@python.org
On Thu, Mar 7, 2013 at 8:50 PM, Lele Gaifax <le...@metapensiero.it> wrote:
> Νίκος Γκρ33κ <nikos...@gmail.com> writes:
>
>> Τη Πέμπτη, 7 Μαρτίου 2013 11:22:09 π.μ. UTC+2, ο χρήστης Lele Gaifax έγραψε:
>>> http://docs.python.org/release/3.0.1/whatsnew/3.0.html#changed-syntax
>>>
>>> 4th (bullet) point.
>>
>> please give me the exact link to read, i cant seem to find it although
>> you send 4th bullet i do not see it....
>
> Oh come on Νίκος, the above *is* the *exact* link to read, I even
> pointed at the right subparagraph: it contains both a link to the
> reference manual for the "except" keyword, and a link to the PEP that
> talks about the change.

I think it's time to take him up on his offer to call him an idiot :)

Nikos, try 'as'.

ChrisA

Joel Goldstick

unread,
Mar 7, 2013, 5:28:12 AM3/7/13
to Νίκος Γκρ33κ, pytho...@python.org

Change from except exc, var to except exc as var. See PEP 3110.


So change the comma to the word 'as'
--
http://mail.python.org/mailman/listinfo/python-list



--

Νίκος Γκρ33κ

unread,
Mar 7, 2013, 5:34:18 AM3/7/13
to pytho...@python.org
Thank you all the trick was to replace the comma with the 'as'

script is almost ready for Python v3.2.3 now!

ima just receivign this error:

--> -->
Traceback (most recent call last):
File "/opt/python3/lib/python3.2/site-packages/MySQL_python-1.2.3-py3.2-linux-x86_64.egg/MySQLdb/cursors.py", line 171, in execute
r = self._query(query)
File "/opt/python3/lib/python3.2/site-packages/MySQL_python-1.2.3-py3.2-linux-x86_64.egg/MySQLdb/cursors.py", line 330, in _query
rowcount = self._do_query(q)
File "/opt/python3/lib/python3.2/site-packages/MySQL_python-1.2.3-py3.2-linux-x86_64.egg/MySQLdb/cursors.py", line 294, in _do_query
db.query(q)
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1")

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "metrites.py", line 212, in <module>
cur.execute( '''SELECT hits FROM counters WHERE url = %s''', (htmlpage,) )
File "/opt/python3/lib/python3.2/site-packages/MySQL_python-1.2.3-py3.2-linux-x86_64.egg/MySQLdb/cursors.py", line 184, in execute
self.errorhandler(self, exc, value)
File "/opt/python3/lib/python3.2/site-packages/MySQL_python-1.2.3-py3.2-linux-x86_64.egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass(errorvalue)
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1")

Tou cna see it more clearly at my website http://superhost.gr

Νίκος Γκρ33κ

unread,
Mar 7, 2013, 5:34:18 AM3/7/13
to comp.lan...@googlegroups.com, pytho...@python.org
Τη Πέμπτη, 7 Μαρτίου 2013 12:19:05 μ.μ. UTC+2, ο χρήστης Chris Angelico έγραψε:

Νίκος Γκρ33κ

unread,
Mar 7, 2013, 5:09:59 AM3/7/13
to comp.lan...@googlegroups.com, pytho...@python.org

Νίκος Γκρ33κ

unread,
Mar 7, 2013, 6:51:51 AM3/7/13
to pytho...@python.org
host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]

Why this line that worked in v2.6 give sme an error in v3.2.3

Traceback (most recent call last):
File "files.py", line 14, in &lt;module&gt;
host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]
File "/opt/python3/lib/python3.2/os.py", line 450, in __getitem__
value = self._data[self.encodekey(key)]
KeyError: b'REMOTE_ADDR'

-->

Νίκος Γκρ33κ

unread,
Mar 7, 2013, 6:51:51 AM3/7/13
to comp.lan...@googlegroups.com, pytho...@python.org
host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]

Why this line that worked in v2.6 give sme an error in v3.2.3

Traceback (most recent call last):

Νίκος Γκρ33κ

unread,
Mar 7, 2013, 7:06:54 AM3/7/13
to Νίκος Γκρ33κ, pytho...@python.org
An these kind of errors iam receiving to please:

Original exception was:
Traceback (most recent call last):
File "pelatologio.py", line 230, in <module>
''') % (clients, hits, money)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 102-108: ordinal not in range(128)

in python v2.6.6 this code works and now i'am using:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

Νίκος Γκρ33κ

unread,
Mar 7, 2013, 7:06:54 AM3/7/13
to comp.lan...@googlegroups.com, Νίκος Γκρ33κ, pytho...@python.org

Chris Angelico

unread,
Mar 7, 2013, 7:17:36 AM3/7/13
to pytho...@python.org
On Thu, Mar 7, 2013 at 11:06 PM, Νίκος Γκρ33κ <nikos...@gmail.com> wrote:
> An these kind of errors iam receiving to please:

Part of being a computer programmer is learning to solve problems.
Dumping piles of problems onto a list like this isn't the best way to
do this. Either pay someone to do the work (there are plenty of web
sites that will help you find freelance programmers), or learn to do
it yourself. Coming to a list like this should be done respectfully -
knowing that you're asking people to devote dev hours for no return -
and preferably in accordance with:

http://www.catb.org/esr/faqs/smart-questions.html

ChrisA
0 new messages