Errin when executing a cgi script that sets a cookie in the browser

  • Thread starter Íéêüëáïò Êïýñáò
  • Start date
Í

Íéêüëáïò Êïýñáò

Since the other thread gone into the wild, i choosed not to participate anylonger and i state the question in this new thread.

'python files.py' interprets without an error.
Problem is that when via browser - http://superhost.gr/cgi-bin/koukos.py
i receive the following:

-------------------------------
root@nikos [/home/nikos/www/cgi-bin]# ls
../ ../ convert.py* files.py* .htaccess koukos.py* metrites.py* pelatologio.py*

root@nikos [/home/nikos/www/cgi-bin]# tail -F /usr/local/apache/logs/error_log &

root@nikos [/home/nikos/www/cgi-bin]# [Wed Jun 05 22:47:43 2013] [error] [client 79.103.41.173] (2)No such file or directory: exec of '/home/nikos/public_html/c gi-bin/koukos.py' failed
[Wed Jun 05 22:47:43 2013] [error] [client 79.103.41.173] Premature end of scrip t headers: koukos.py
----------------------------------

What file does the error complain it cannot find? I do not understand its message. Here is the code of koukos.py


-----------------------------
#!/usr/bin/python
# coding=utf-8

import cgitb; cgitb.enable()
import cgi, os, sys, locale, codecs
from http import cookies

#needed line, script does *not* work without it
sys.stdout = codecs.getwriter('utf-8')(sys.stdout.detach())

# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
nikos = cookie.get('nikos')

# if visitor cookie does exist
if nikos:
message = "ÁÐÏ ÔÇÍ ÅÐÏÌÅÍÇ ÅÐÉÓÊÅØÇ ÓÏÕ ÈÁ ÓÅ ÕÐÏËÏÃÉÆÙ ÙÓ ÅÐÉÓÊÅÐÔÇ ÁÕÎÁÍÏÍÔÁÓ ÔÏÍ ÌÅÔÑÇÔÇ!"
cookie['nikos'] = 'admin'
cookie['nikos']['path'] = '/'
cookie['nikos']['expires'] = -1 #this cookie will expire now
else:
message = "ÁÐÏ ÄÙ ÊÁÉ ÓÔÏ ÅÎÇÓ ÄÅÍ ÓÅ ÅÉÄÁ, ÄÅÍ ÓÅ ÎÅÑÙ, ÄÅÍ ÓÅ ÁÊÏÕÓÁ! ÈÁ ÅÉÓÁÉ ÐËÅÏÍ Ï ÁÏÑÁÔÏÓ ÅÐÉÓÊÅÐÔÇÓ!!"
cookie['nikos'] = 'admin'
cookie['nikos']['path'] = '/'
cookie['nikos']['expires'] = 60*60*24*30*12 #this cookie will expire ina year


print( cookie, "Content-type: text/html; charset=utf-8\n", message )

sys.exit(0)
 
J

John Gordon

In said:
'python files.py' interprets without an error.
Problem is that when via browser - http://superhost.gr/cgi-bin/koukos.py
i receive the following:

Why should 'files.py' have any relation to 'koukous.py'?
What file does the error complain it cannot find? I do not understand its
message. Here is the code of koukos.py

Does /usr/bin/python exist? Scripts can throw a 'No such file or directory'
or 'Command not found' error if they begin with a shebang line which refers
to a nonexistent program.
 
C

Chris Angelico

print( cookie, "Content-type: text/html; charset=utf-8\n", message )

Do you know what this does?

Try it at the console. See what it outputs.

ChrisA
 
R

rurpy

...
print( cookie, "Content-type: text/html; charset=utf-8\n", message )
...

If you look in the Apache error log file, you will see something like,

[Wed Jun 05 16:39:14 2013] [error] [client 192.168.0.1] malformed header from script. Bad header= \xce\x91\xce\xa0\xce\x9f \xce\x94\xce\xa9 \xce\x9a\xce\x91\xce\x99 \xce\xa3\xce\xa4\xce\x9f \xce\x95\xce: koukos.py

which is saying that the 'message' text is being interpreted as
being part of the headers.

You are missing a blank line between the header lines and the
page text. That is, I think you want,

print( cookie, "Content-type: text/html; charset=utf-8\n\n", message )

(ie, note the two \n's after the "utf-8" test.)
 
R

rurpy

But that won't solve it either. The default separator for print is a
space, so this will indent his Content-type line by one space.

Ah, quite right. Something like

print( cookie, "\nContent-type: text/html; charset=utf-8\n\n", message )

then.
 
C

Chris Angelico

Ah, quite right. Something like

print( cookie, "\nContent-type: text/html; charset=utf-8\n\n", message )

then.

Or change the sep, or concatenate with + instead of using , between
them. Or put them on separate lines. Anything like that would work.
And it's really easy to try things out interactively to see what
they'll do...

ChrisA
 
R

rurpy

On Wednesday, June 5, 2013 3:03:29 PM UTC-6, Chris Angelico wrote: ...[...]
Ah, quite right. Something like

print( cookie, "\nContent-type: text/html; charset=utf-8\n\n", message )

then.

Or change the sep, or concatenate with + instead of using , between
them. Or put them on separate lines. Anything like that would work.

Of course.
And it's really easy to try things out interactively to see what
they'll do...

Sure, once one makes the connection between "Server Error" and missing "\n"
which is where Îικόλαος was stuck I'm guessing.
 
C

Cameron Simpson

| On Wednesday, June 5, 2013 3:03:29 PM UTC-6, Chris Angelico wrote:
| > > On Wednesday, June 5, 2013 1:54:45 PM UTC-6, Îικόλαος ΚοÏÏας wrote:
| > >>...
| > >> print( cookie, "Content-type: text/html; charset=utf-8\n", message )
| > >>...
| > > print( cookie, "Content-type: text/html; charset=utf-8\n\n", message )
| > > (ie, note the two \n's after the "utf-8" test.)
| >
| > But that won't solve it either. The default separator for print is a
| > space, so this will indent his Content-type line by one space.
|
| Ah, quite right. Something like
|
| print( cookie, "\nContent-type: text/html; charset=utf-8\n\n", message )
|
| then.

Unless "cookie" already has a newline. Then you'll end the headers there:)

A more robust approach might be to build a dict (or possibly better,
list) of headers without newlines and then as a separate act to
print them with newlines and add the spacer newline later, before
writing the message body.

Cheers,
--
Cameron Simpson <[email protected]>

Drill for oil? You mean drill into the ground to try and find oil?
You're crazy.
--Drillers whom Edwin L. Drake tried to enlist to his project
to drill for oil in 1859.
 
C

Chris Angelico

Sure, once one makes the connection between "Server Error" and missing "\n"
which is where Íéêüëáïò was stuck I'm guessing.

I know that's a bit of a jump. That's why, right back when he first
posted his problem, I quoted _that one line_ and pointed him to the
interactive interpreter. I'm pretty sure he still isn't reading my
posts... or, most likely, anyone's.

ChrisA
 
Í

Íéêüëáïò Êïýñáò

Ôç ÐÝìðôç, 6 Éïõíßïõ 2013 12:18:39 ð.ì. UTC+3, ï ÷ñÞóôçò (e-mail address removed) Ýãñáøå:
Ah, quite right. Something like



print( cookie, "\nContent-type: text/html; charset=utf-8\n\n", message )



then.


print( cookie, "\nContent-type: text/html; charset=utf-8\n\n", message )

or by trying:

print( cookie + "\nContent-type: text/html; charset=utf-8\n\n" + message )

the output is for both:

(e-mail address removed) [~]# tail -F /usr/local/apache/logs/error_log &

(e-mail address removed) [~]# [Thu Jun 06 06:20:11 2013] [error] [client 79.103.41.173] (2)No such file or directory: exec of '/home/nikos/public_html/cgi-bin/koukos.py' failed
[Thu Jun 06 06:20:11 2013] [error] [client 79.103.41.173] Premature end of script headers: koukos.py
 
Í

Íéêüëáïò Êïýñáò

Ôç ÐÝìðôç, 6 Éïõíßïõ 2013 1:21:08 ð.ì. UTC+3, ï ÷ñÞóôçò Chris Angelico Ýãñáøå:
Or change the sep, or concatenate with + instead of using , between

them. Or put them on separate lines. Anything like that would work.

And it's really easy to try things out interactively to see what

they'll do...



ChrisA

Thi is failing also with same error:

print( cookie )
print( '''Content-type: text/html; charset=utf-8\n''' )
print( message )
 
Í

Íéêüëáïò Êïýñáò

root@nikos [~]# chmod 755 /var/log
root@nikos [~]# chmod 755 /var/log/httpd
root@nikos [~]# chmod 666 /var/log/httpd/suexec.log

root@nikos [~]# chmod 755 /usr/local/apache
root@nikos [~]# chmod 755 /usr/local/apache/logs/
root@nikos [~]# chmod 666 /usr/local/apache/logs/error_log


and then execute via browser: http://superhost.gr/cgi-bin/koukos.py
still same error appearing:

[Thu Jun 06 09:23:54 2013] [error] [client 79.103.41.173] suexec failure: could not open log file
[Thu Jun 06 09:23:54 2013] [error] [client 79.103.41.173] fopen: Permission denied
[Thu Jun 06 09:23:54 2013] [error] [client 79.103.41.173] Premature end of script headers: koukos.py
 
Í

Íéêüëáïò Êïýñáò

I have re-enabled 'suexec' and set cgi as default phphandler and then trying:

print( cookie )
print( '''Content-type: text/html; charset=utf-8\n''' )
print( message )

---------------------------------
(e-mail address removed) [~/www/data/apps]# [Thu Jun 06 13:51:28 2013] [error] [client 79.103.41.173] suexec failure: could not open log file
[Thu Jun 06 13:51:28 2013] [error] [client 79.103.41.173] fopen: Permission denied
[Thu Jun 06 13:51:28 2013] [error] [client 79.103.41.173] Premature end of script headers: koukos.py

Even if dissable/enable suexec still this output error.
 
R

rurpy

print( cookie )
print( '''Content-type: text/html; charset=utf-8\n''' )
print( message )

---------------------------------
(e-mail address removed) [~/www/data/apps]# [Thu Jun 06 13:51:28 2013] [error] [client 79.103.41.173] suexec failure: could not open log file
[Thu Jun 06 13:51:28 2013] [error] [client 79.103.41.173] fopen: Permission denied
[Thu Jun 06 13:51:28 2013] [error] [client 79.103.41.173] Premature end of script headers: koukos.py

Even if dissable/enable suexec still this output error.

This is only a guess but...

The permissions on koukos.py have to be exactly right.
Obviously if they are too restrictive Apache won't be
able to read or execute it. But they can't be to open
either -- in particular the file must have execute
permission and must not have write permission for either
group or others (write for user only).

If the permissions are too open, Apache will try to write
an error message to suexec.log. I suspect that your
permissions (or file owner) is wrong on your suexec.log
file (or containing directory) and that is the cause of
the "could not open log file " message.

So I think you have too problems: wrong permissions on
koukos.py and wrong owner or permissions on suexec.log.

For reference this is working here:
-rw-r--r-- 1 apache apache 314 Jun 6 12:19 /var/log/httpd/suexec.log

-rwxr-xr-x 1 me me 1113 Jun 5 14:40 koukos.py

You'll need to adjust things for your particular Apache
environment.

As I said, this is only a guess. Hope it helps.
 
Í

Íéêüëáïò Êïýñáò

Ôç ÐÝìðôç, 6 Éïõíßïõ 2013 9:40:04 ì.ì. UTC+3, ï ÷ñÞóôçò (e-mail address removed) Ýãñáøå:
print( cookie )
print( '''Content-type: text/html; charset=utf-8\n''' )
print( message )
---------------------------------

(e-mail address removed) [~/www/data/apps]# [Thu Jun 06 13:51:28 2013] [error] [client 79.103.41.173] suexec failure: could not open log file
[Thu Jun 06 13:51:28 2013] [error] [client 79.103.41.173] fopen: Permission denied
[Thu Jun 06 13:51:28 2013] [error] [client 79.103.41.173] Premature endof script headers: koukos.py
Even if dissable/enable suexec still this output error.



This is only a guess but...



The permissions on koukos.py have to be exactly right.

Obviously if they are too restrictive Apache won't be

able to read or execute it. But they can't be to open

either -- in particular the file must have execute

permission and must not have write permission for either

group or others (write for user only).



If the permissions are too open, Apache will try to write

an error message to suexec.log. I suspect that your

permissions (or file owner) is wrong on your suexec.log

file (or containing directory) and that is the cause of

the "could not open log file " message.



So I think you have too problems: wrong permissions on

koukos.py and wrong owner or permissions on suexec.log.



For reference this is working here:

-rw-r--r-- 1 apache apache 314 Jun 6 12:19 /var/log/httpd/suexec.log



-rwxr-xr-x 1 me me 1113 Jun 5 14:40 koukos.py



You'll need to adjust things for your particular Apache

environment.



As I said, this is only a guess. Hope it helps.

Hello! thanks for trying to help.
Here they are:

(e-mail address removed) [~/www/cgi-bin]# ls -l koukos.py
-rwxr-xr-x 1 nikos nikos 1160 Jun 6 06:27 koukos.py*

(e-mail address removed) [~/www/cgi-bin]# ls -l /var/log/httpd/suexec.log
-rw-rw-rw- 1 root apache 0 Jun 1 02:52 /var/log/httpd/suexec.log

(e-mail address removed) [~/www/cgi-bin]# ls -l /usr/local/apache/logs/suexec_log
-rw-rw-r-- 1 root apache 675097 Jun 6 21:43 /usr/local/apache/logs/suexec_log


I have applied to them the group 'apache' so Apache User can utilize them.

But its still a wonder to me why two different suexec logs exist.
Please tell me wht else you want me to try.
 
Í

Íéêüëáïò Êïýñáò

Since cPanel is in charge of apache i ahve even:

chown nobody:nobody to both of the suexec logs.

(e-mail address removed) [~/www/cgi-bin]# ls -l /usr/local/apache/logs/suexec_log
-rw-rw-r-- 1 nobody nobody 675389 Jun 6 22:05 /usr/local/apache/logs/suexec_log
(e-mail address removed) [~/www/cgi-bin]# ls -l /var/log/httpd/su*
-rw-rw-rw- 1 nobody nobody 0 Jun 1 02:52 /var/log/httpd/suexec_log


Still same error :(
 
L

Lele Gaifax

Îικόλαος ΚοÏÏας said:
I have re-enabled 'suexec' and set cgi as default phphandler and then trying:

print( cookie )
print( '''Content-type: text/html; charset=utf-8\n''' )
print( message )

Did you tried running that by a standalone Python interpreter? Did you
notice something strange, something like that an empty line is missing
between headers and body?

ciao, lele.
 
Í

Íéêüëáïò Êïýñáò

Ôç ÐÝìðôç, 6 Éïõíßïõ 2013 10:26:08 ì.ì. UTC+3, ï ÷ñÞóôçò Lele Gaifax Ýãñáøå:
Did you tried running that by a standalone Python interpreter? Did you
notice something strange, something like that an empty line is missing
between headers and body?

No, nothing at all.
Two '/n/n' are not required. Months now the way i'm printing headers is by:

print( '''Content-type: text/html; charset=utf-8\n''' )

and the scripts owrk correctly in browser and in python interpreter too.
 
S

Skip Montanaro

Did you tried running that by a standalone Python interpreter? Did you
notice something strange, something like that an empty line is missing
between headers and body?

He will get an extra blank line, since he added a newline character at
the end of his Content-Type string.

Skip
 
Í

Íéêüëáïò Êïýñáò

Something else i need to try so for 'suexec' to be able to open its own lof file?

What a weird error that is. An Apache's extension that can open its own log file.....
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top