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

Discussion in 'Python' started by Íéêüëáïò Êïýñáò, Jun 5, 2013.

  1. 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)
    ---------------------------------

    All it tries to do is to set a cookie.
    Íéêüëáïò Êïýñáò, Jun 5, 2013
    #1
    1. Advertising

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

    John Gordon Guest

    In <> =?ISO-8859-7?B?zenq/Ovh7/Igyu/98eHy?= <> writes:

    > '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


    > -----------------------------
    > #!/usr/bin/python


    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.

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Jun 5, 2013
    #2
    1. Advertising

  3. Re: Errin when executing a cgi script that sets a cookie in thebrowser

    On Thu, Jun 6, 2013 at 5:54 AM, Íéêüëáïò Êïýñáò<> wrote:
    > 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
    Chris Angelico, Jun 5, 2013
    #3
  4. Íéêüëáïò Êïýñáò

    Guest

    On Wednesday, June 5, 2013 1:54:45 PM UTC-6, Îικόλαος ΚοÏÏας wrote:
    >...
    > 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.)
    , Jun 5, 2013
    #4
  5. Íéêüëáïò Êïýñáò

    Guest

    On Wednesday, June 5, 2013 3:03:29 PM UTC-6, Chris Angelico wrote:
    > On Thu, Jun 6, 2013 at 6:56 AM, <> 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.
    , Jun 5, 2013
    #5
  6. Re: Errin when executing a cgi script that sets a cookie in thebrowser

    On Thu, Jun 6, 2013 at 7:18 AM, <> wrote:
    > On Wednesday, June 5, 2013 3:03:29 PM UTC-6, Chris Angelico wrote:
    >> On Thu, Jun 6, 2013 at 6:56 AM, <> 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.


    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
    Chris Angelico, Jun 5, 2013
    #6
  7. Íéêüëáïò Êïýñáò

    Guest

    On 06/05/2013 04:21 PM, Chris Angelico wrote:
    > On Thu, Jun 6, 2013 at 7:18 AM, <> wrote:
    >> 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.
    , Jun 5, 2013
    #7
  8. Re: Errin when executing a cgi script that sets a cookie in thebrowser

    On 05Jun2013 14:18, <> wrote:
    | On Wednesday, June 5, 2013 3:03:29 PM UTC-6, Chris Angelico wrote:
    | > On Thu, Jun 6, 2013 at 6:56 AM, <> 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 <>

    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.
    Cameron Simpson, Jun 6, 2013
    #8
  9. Re: Errin when executing a cgi script that sets a cookie in thebrowser

    On Thu, Jun 6, 2013 at 8:36 AM, <> wrote:
    >> 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.


    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
    Chris Angelico, Jun 6, 2013
    #9
  10. Ôç ÐÝìðôç, 6 Éïõíßïõ 2013 12:18:39 ð.ì. UTC+3, ï ÷ñÞóôçò Ýãñáøå:
    > On Wednesday, June 5, 2013 3:03:29 PM UTC-6, Chris Angelico wrote:
    >
    > > On Thu, Jun 6, 2013 at 6:56 AM, <> 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.



    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:

    [~]# tail -F /usr/local/apache/logs/error_log &

    [~]# [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
    Íéêüëáïò Êïýñáò, Jun 6, 2013
    #10
  11. Ôç ÐÝìðôç, 6 Éïõíßïõ 2013 1:21:08 ð.ì. UTC+3, ï ÷ñÞóôçò Chris Angelico Ýãñáøå:
    > On Thu, Jun 6, 2013 at 7:18 AM, <> wrote:
    >
    > > On Wednesday, June 5, 2013 3:03:29 PM UTC-6, Chris Angelico wrote:

    >
    > >> On Thu, Jun 6, 2013 at 6:56 AM, <> 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.

    >
    >
    >
    > 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 )
    Íéêüëáïò Êïýñáò, Jun 6, 2013
    #11
  12. 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
    Íéêüëáïò Êïýñáò, Jun 6, 2013
    #12
  13. 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 )

    ---------------------------------
    [~/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.
    Íéêüëáïò Êïýñáò, Jun 6, 2013
    #13
  14. Íéêüëáïò Êïýñáò

    Guest

    On 06/06/2013 04:53 AM, Îικόλαος ΚοÏÏας wrote:> I have re-enabled 'suexec' andset cgi as default phphandler and then trying:
    >
    > print( cookie )
    > print( '''Content-type: text/html; charset=utf-8\n''' )
    > print( message )
    >
    > ---------------------------------
    > [~/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.
    , Jun 6, 2013
    #14
  15. Ôç ÐÝìðôç, 6 Éïõíßïõ 2013 9:40:04 ì.ì. UTC+3, ï ÷ñÞóôçò Ýãñáøå:
    > On 06/06/2013 04:53 AM, Íéêüëáïò Êïýñáò wrote:> 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 )

    >
    > >

    >
    > > ---------------------------------

    >
    > > [~/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:

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

    [~/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

    [~/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.
    Íéêüëáïò Êïýñáò, Jun 6, 2013
    #15
  16. Since cPanel is in charge of apache i ahve even:

    chown nobody:nobody to both of the suexec logs.

    [~/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
    [~/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 :(
    Íéêüëáïò Êïýñáò, Jun 6, 2013
    #16
  17. Íéêüëáïò Êïýñáò

    Lele Gaifax Guest

    Re: Errin when executing a cgi script that sets a cookie in thebrowser

    Îικόλαος ΚοÏÏας <> writes:

    > 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.
    --
    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, Jun 6, 2013
    #17
  18. Ôç ÐÝìðôç, 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.
    Íéêüëáïò Êïýñáò, Jun 6, 2013
    #18
  19. Re: Errin when executing a cgi script that sets a cookie in thebrowser

    > 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
    Skip Montanaro, Jun 6, 2013
    #19
  20. 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.....
    Íéêüëáïò Êïýñáò, Jun 6, 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. Ben
    Replies:
    3
    Views:
    5,830
    Steven Cheng[MSFT]
    Jun 3, 2004
  2. Jimbo
    Replies:
    1
    Views:
    655
    Pierre Quentel
    Apr 8, 2010
  3. Xeno Campanoli
    Replies:
    2
    Views:
    136
    Xeno Campanoli
    Jul 10, 2008
  4. Tom Salzmann
    Replies:
    3
    Views:
    259
  5. kath
    Replies:
    4
    Views:
    561
    J. Gleixner
    Apr 9, 2007
Loading...

Share This Page