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:

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

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

    [email protected] [/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)
     
    Íéêüëáïò Êïýñáò, Jun 5, 2013
    #1
    1. Advertisements

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

    John Gordon Guest

    Why should 'files.py' have any relation to 'koukous.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.
     
    John Gordon, Jun 5, 2013
    #2
    1. Advertisements

  3. Do you know what this does?

    Try it at the console. See what it outputs.

    ChrisA
     
    Chris Angelico, Jun 5, 2013
    #3
  4. Íéêüëáïò Êïýñáò

    rurpy Guest

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

    rurpy Guest

    Ah, quite right. Something like

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

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

    rurpy Guest

    Of course.
    Sure, once one makes the connection between "Server Error" and missing "\n"
    which is where Îικόλαος was stuck I'm guessing.
     
    rurpy, Jun 5, 2013
    #7
  8. | 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 <>

    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. 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, ï ÷ñÞóôçò Ýãñáøå:

    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 Ýãñáøå:
    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. [email protected] [~]# chmod 755 /var/log
    [email protected] [~]# chmod 755 /var/log/httpd
    [email protected] [~]# chmod 666 /var/log/httpd/suexec.log

    [email protected] [~]# chmod 755 /usr/local/apache
    [email protected] [~]# chmod 755 /usr/local/apache/logs/
    [email protected] [~]# 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. Íéêüëáïò Êïýñáò

    rurpy Guest

    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.
     
    rurpy, Jun 6, 2013
    #14
  15. Ôç ÐÝìðôç, 6 Éïõíßïõ 2013 9:40:04 ì.ì. UTC+3, ï ÷ñÞóôçò Ýãñáøå:
    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

    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.
     
    Lele Gaifax, Jun 6, 2013
    #17
  18. Ôç ÐÝìðôç, 6 Éïõíßïõ 2013 10:26:08 ì.ì. UTC+3, ï ÷ñÞóôçò Lele Gaifax Ýãñáøå:
    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. Did you tried running that by a standalone Python interpreter? Did you
    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. Advertisements

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 (here). After that, you can post your question and our members will help you out.