Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Trying to set a cookie within a python script

Reply
Thread Tools

Trying to set a cookie within a python script

 
 
Dave Angel
Guest
Posts: n/a
 
      08-04-2010


¯º¿Â wrote:
>> On 3 Αύγ, 21:00, Dave Angel <(E-Mail Removed)> wrote:
>>

>
>
>> A string is an object containing characters. A string literal is one of
>> the ways you create such an object. When you create it that way, you
>> need to make sure the compiler knows the correct encoding, by using the
>> encoding: line at beginning of file.
>>

>
>
> mymessage = "καλημ*ρα" <==== string
> mymessage = u"καλημ*ρα" <==== string literal?
>
> So, a string literal is one of the encodings i use to create a string
> object?
>
>

No, both lines take a string literal, create an object, and bind a name
to that object. In the first case, the object is a string, and in the
second it's a unicode-string. But the literal is the stuff after the
equals sign in both these cases.

Think about numbers for a moment. When you say
salary = 4.1

you've got a numeric literal that's three characters long, and a name
that's six characters long. When the interpreter encounters this line,
it builds an object of type float, whose value approximates 4.1,
according to the language rules. It then binds the name salary to this
object.

> Can the encodign of a python script file be in iso-8859-7 which means
> the file contents is saved to the hdd as greek-iso but the part of
> this variabel value mymessage ="καλημ*ρα" is saved as utf-8 ot the
> opposite?
>
>

A given file needs to have a single encoding, or you're in big trouble.
So a script file is encoded by the text editor in a single encoding
method, which is not saved to the file (except indirectly if you specify
BOM). It's up to you to add a line to the beginning to tell Python how
to decode the file. One decoding for one file.
> have the file saved as utf-8 but one variuable value as greek
> encoding?
>
>

Variables are not saved to source (script) files. Literals are in the file.
> Encodings still give me headaches. I try to understand them as
> different ways to store data in a media.
>
> Tell me something. What encoding should i pick for my scripts knowing
> that only contain english + greek chars??
> iso-8859-7 or utf-8 and why?
>
>

Depends on how sure you are that your program will never need characters
outside your greek character set. Remember Y2K?

> Can i save the sting lets say "Νίκος" in different encodings and still
> print out correctly in browser?
>
> ascii =he standard english character set only, right?
>
>
>> The web server wraps a few characters before and after your html stream,
>> but it shouldn't touch the stream itself.
>>

>
> So the pythoon compiler using the cgi module is the one that is
> producing the html output that immediately after send to the web
> server, right?
>
>
>
>>> For example if i say mymessage =καλημ*ρα" and the i say mymessage = u"καλημ*ρα" then the 1st one is a greek encoding variable while the
>>> 2nd its a utf-8 one?
>>>

>> No, the first is an 8 bit copy of whatever bytes your editor happened to
>> save.
>>

>
> But since mymessage =καλημ*ρα" is a string containing greek
> characaters why the editor doesn't save it as such?
>
>

Because the editor is editing text, not python objects. It's job is
solely to represent all your keystrokes in some consistent manner so
that they can be interpreted later by some other program, possibly a
compiler.
> It reminds me of varibles an valeus where if you say
>
> a = 5, a var becomes instantly an integer variable
> while
> a = 'hello' , become instantly a string variable
>
>
>
>> mymessage = u"καλημ*ρα"
>>
>> creates an object that is *not* encoded.
>>

>
> Because it isn't saved by the editor yet? In what satet is this object
> in before it gets encoded?
> And it egts encoded the minute i tell the editor to save the file?
>
>

You're confusing timeframes here. Notepad++ doesn't know Python, and
it's long gone by the time the compiler deals with that line. In
Notepad++, there are no python objects, encoded or not.
>> Encoding is taking the unicode
>> stream and representing it as a stream of bytes, which may or may have
>> more bytes than the original has characters.
>>

>
>
> So this line mymessage = u"καλημ*ρα" what it does is tell the browser
> thats when its time to save the whole file to save this string as
> utf-8?
>
>

No idea what you mean. The browser isn't saving anything; it doesn't
even get involved till after the python code has completed.
> If yes, then if were to save the above string as greek encoding how
> was i suppose to right it?
>
> Also if u ise the 'coding line' in the beggining of the file is there
> a need for using the u literal?
>
>

If you don't use the u literal, then don't even try to use utf-8. You'll
find that strings have the wrong lengths, and therefore subscripts and
formatting will sometimes fail in strange ways.
>> I personally haven't done any cookie code. If I were debugging this, I'd
>> factor out the multiple parts of that if statement, and find out which
>> one isn't true. From here I can't guess.
>>

>
> I did what you say and foudn out that both of the if condition parts
> were always false thast why the if code blck never got executed.
>
> And it is alwsy wrong because the cookie never gets set.
>
> So can you please tell me why this line
>
> cookie['visitor'] = 'nikos', time() + 60*60*24*365 ) #this cookie
> will expire in an year
>
> never created a cookie?
>
>

As I said, I've never coded with cookies. But to create a cookie, you
have to communicate with a browser, and that takes lots more than just
adding an item to a map. Further, your getenv() will normally give you
the state of the environment at the time your program was launched, so I
wouldn't expect it to change.

If I had to guess how cookies are done in CGI, I'd say that you probably
have to talk to the CGI server and terminate, and that afterwards
there'd be a new launch of your code, from which you could check that
environment variable to see the results of the cookie.


DaveA
 
Reply With Quote
 
 
 
 
Dotan Cohen
Guest
Posts: n/a
 
      08-04-2010
On Wed, Aug 4, 2010 at 18:30, Dave Angel <(E-Mail Removed)> wrote:
> Depends on how sure you are that your program will never need characters
> outside your greek character set. Remember Y2K?
>


Don't forget that the Euro symbol is outside the Greek character set.

--
Dotan Cohen

http://gibberish.co.il
http://what-is-what.com
 
Reply With Quote
 
 
 
 
donn
Guest
Posts: n/a
 
      08-04-2010
On 04/08/2010 20:09, Dotan Cohen wrote:
> Don't forget that the Euro symbol is outside the Greek character set.

I could make some kind of economic joke here, but I'm also broke

\d
 
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
Simple Cookie Script: Not recognising Cookie Jimbo Python 1 04-08-2010 07:34 AM
How to set a cookie using Cookie Module tryg.olson@gmail.com Python 2 01-08-2009 02:39 PM
Is there a way to set a cookie with CGI::Cookie objects, from anrhtml file? Xeno Campanoli Ruby 2 07-09-2008 11:07 PM
Cookie Confusion - How to Set a Cookie cbhoem@gmail.com Python 5 05-02-2008 02:56 PM
Cookies set one time, I delete cookie, cookie is never set again! Phil Powell Javascript 2 09-05-2003 02:17 PM



Advertisments