Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Errin when executing a cgi script that sets a cookie in the browser

Reply
Thread Tools

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

 
 
Νικόλαος Κούρας
Guest
Posts: n/a
 
      06-05-2013
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.
 
Reply With Quote
 
 
 
 
John Gordon
Guest
Posts: n/a
 
      06-05-2013
In <(E-Mail Removed)> =?ISO-8859-7?B?zenq/Ovh7/Igyu/98eHy?= <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      06-05-2013
On Thu, Jun 6, 2013 at 5:54 AM, Νικόλαος Κούρας<(E-Mail Removed)> 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
 
Reply With Quote
 
rurpy@yahoo.com
Guest
Posts: n/a
 
      06-05-2013
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.)
 
Reply With Quote
 
rurpy@yahoo.com
Guest
Posts: n/a
 
      06-05-2013
On Wednesday, June 5, 2013 3:03:29 PM UTC-6, Chris Angelico wrote:
> On Thu, Jun 6, 2013 at 6:56 AM, <(E-Mail Removed)> 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.
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      06-05-2013
On Thu, Jun 6, 2013 at 7:18 AM, <(E-Mail Removed)> wrote:
> On Wednesday, June 5, 2013 3:03:29 PM UTC-6, Chris Angelico wrote:
>> On Thu, Jun 6, 2013 at 6:56 AM, <(E-Mail Removed)> 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
 
Reply With Quote
 
rurpy@yahoo.com
Guest
Posts: n/a
 
      06-05-2013
On 06/05/2013 04:21 PM, Chris Angelico wrote:
> On Thu, Jun 6, 2013 at 7:18 AM, <(E-Mail Removed)> 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.
 
Reply With Quote
 
Cameron Simpson
Guest
Posts: n/a
 
      06-06-2013
On 05Jun2013 14:18, (E-Mail Removed) <(E-Mail Removed)> wrote:
| On Wednesday, June 5, 2013 3:03:29 PM UTC-6, Chris Angelico wrote:
| > On Thu, Jun 6, 2013 at 6:56 AM, <(E-Mail Removed)> 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 <(E-Mail Removed)>

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.
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      06-06-2013
On Thu, Jun 6, 2013 at 8:36 AM, <(E-Mail Removed)> 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
 
Reply With Quote
 
Νικόλαος Κούρας
Guest
Posts: n/a
 
      06-06-2013
Τη Πέμπτη, 6 Ιουνίου 2013 12:18:39 π.μ. UTC+3, ο χρήστης (E-Mail Removed) έγραψε:
> On Wednesday, June 5, 2013 3:03:29 PM UTC-6, Chris Angelico wrote:
>
> > On Thu, Jun 6, 2013 at 6:56 AM, <(E-Mail Removed)> 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:

(E-Mail Removed) [~]# tail -F /usr/local/apache/logs/error_log &

(E-Mail 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What was the project that made you feel skilled in Python? Ned Batchelder Python 2 05-20-2013 03:16 PM
Re: What was the project that made you feel skilled in Python? Chris Angelico Python 0 05-19-2013 11:48 AM
executing python scripts that are symlinked Charles Smith Python 6 05-16-2013 02:45 PM
When did the bool type make it into the language? Christopher Pisz C++ 13 05-05-2013 03:34 PM
Windows 8 - so bad it's hastening the death of the PC? ~misfit~ NZ Computing 18 04-15-2013 04:15 AM



Advertisments