Trying to set a cookie within a python script

Dave Angel davea at ieee.org
Tue Aug 3 10:10:15 CEST 2010


��������������������������������� wrote:
>> On 2 Αύγ, 23:57, Thomas Jollans <thomas at jollans.com> wrote:
>>     
>
>   
>> So: tripple-check that
>>
>>  * your file is <insert encoding here (aka UTF-8)>
>>  * Python knows that
>>  * the web browser knows that
>>     
>
> Thank you! i used print ''' Content-Type: text/html; charset=F-8 /
> n''' and it worked.
> I'am still pretty confused about the encodings.
>
> Please tell me the difference between 3 things.
>
> a) Asking Notepad++(my editor) to save all my python scripts as UTF-8
> without BOM.
> b) Using this line '# -*- coding: utf-8 -*-' Isn't this line supposed
> to tell browser that the contents of this python script as in UTF-8
> and to handle it as such?
> c) print ''' Content-Type: text/html; charset=F-8 /n'''
>
> Please explain to me as simple as you can because from the time with
> php and perl encodings not only gave me a hard time but also caused my
> program to produce internal server errors so i need to understand the
> differences.
>
> <snip>
There actually should be more than 3 things here.  To understand the 
distinctions you have to see who handles which data, and how.

a) a text editor takes keystrokes and cut/paste info and other data, and 
produces a stream of (unicode) characters.  It then encodes each of  
those character into one or more bytes and saves it to a file.  You have 
to tell Notepad++ how to do that encoding.  Note that unless it's saving 
a BOM, there's no clue in the file what encoding it used.

b) The python compiler has to interpret the bytes it finds (spec. within 
string literals and comments), and decode them into unicode for its own 
work.  It uses the 'coding:' comment to decide how to do this.  But once 
the file has been compiled, that comment is totally irrelevant, and ignored.

c1) Your python code has to decide how to encode its information when 
writing to stdout.  There are several ways to accomplish that.

c2) The browser sees only what was sent to stdout, starting with the 
"Content-Type..." line.  It uses that line to decide how to decode the 
rest of the stream.  Let me reemphasize, the browser does not see any of 
the python code, or comments.

DaveA




More information about the Python-list mailing list