Select fails when cookie tried to get a numeric value

Ned Batchelder ned at nedbatchelder.com
Sat Oct 5 12:42:21 EDT 2013


On 10/5/13 12:17 PM, Νίκος Αλεξόπουλος wrote:
> Στις 5/10/2013 7:14 μμ, ο/η Νίκος Αλεξόπουλος έγραψε:
>> Στις 5/10/2013 7:08 μμ, ο/η Ned Batchelder έγραψε:
>>>
>>> On 10/5/13 11:52 AM, Νίκος Αλεξόπουλος wrote:
>>>> Στις 5/10/2013 6:12 μμ, ο/η Ned Batchelder έγραψε:
>>>>> On 10/5/13 10:40 AM, Νίκος Αλεξόπουλος wrote:
>>>>>> Στις 5/10/2013 4:53 μμ, ο/η Ned Batchelder έγραψε:
>>>>>>
>>>>>>>  From reading the bottom-most frame, you can see that the 
>>>>>>> problem is
>>>>>>> that "val" is an http.cookies.Morsel object.  This means you 
>>>>>>> probably
>>>>>>> tried to use a cookie object as data in your SQL query, and MySQL
>>>>>>> doesn't know what to do with that object.  You'll have to use a 
>>>>>>> more
>>>>>>> primitive piece of data in your query.
>>>>>>
>>>>>> # initialize cookie
>>>>>> cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
>>>>>> cookie.load( cookie )
>>>>>> cookieID = cookie.get('ID')
>>>>>>
>>>>>> # if browser cookie does not exist, set it
>>>>>> if not cookieID:
>>>>>>     cookie['ID'] = random.randrange(0, 10000)
>>>>>>     cookie['ID']['path'] = '/'
>>>>>>     cookie['ID']['expires'] = 60*60*24*365        #this cookie will
>>>>>> expire in a month
>>>>>>     cookieID = cookie.get('ID')
>>>>>>     print( cookie )
>>>>>>
>>>>>>
>>>>>> In the above code i try to retrive the cookie form the visitor's
>>>>>> browser and if it does nto exist i create one.
>>>>>>
>>>>>>
>>>>>>
>>>>>> For some reason i think CookieID nevers gets inserted itnot the
>>>>>> database that's why mysql's select statemnt fails.
>>>>>>
>>>>>> When i print CookieID i was under the impression i would see a 
>>>>>> random
>>>>>> number like '5369' but instead it display the follwong.
>>>>>>
>>>>>> Set-Cookie: ID="Set-Cookie: ID=5369"
>>>>>>
>>>>>> The mysql filed CookieID is of datatype's int(5) so it cannto store
>>>>>> this value.
>>>>>>
>>>>>> If iam correct and thi is trully the problem then how can i just get
>>>>>> the random number part out the whole string?
>>>>>>
>>>>>> Do you see something wrong?
>>>>>> Why cookie['ID'] retuned this string back and not just the number?
>>>>>>
>>>>>>
>>>>>
>>>>> Thanks for being patient.  Where you have this:
>>>>>
>>>>>      cookieID = cookie.get('ID')
>>>>>
>>>>> you actually want this:
>>>>>
>>>>>      cookieID = cookie.get('ID').value
>>>>>
>>>>> --Ned.
>>>>
>>>>
>>>> [Sat Oct 05 15:51:02 2013] [error] [client 108.162.229.114]   File
>>>> "/home/nikos/public_html/cgi-bin/metrites.py", line 84, in <module>
>>>> [Sat Oct 05 15:51:02 2013] [error] [client 108.162.229.114] cookieID =
>>>> cookie.get('ID').value
>>>> [Sat Oct 05 15:51:02 2013] [error] [client 108.162.229.114]
>>>> AttributeError: 'NoneType' object has no attribute 'value'
>>>>
>>>
>>> Nikos: you know enough to understand what is going on here.
>>>
>>> This list will not serve you well if you take every error message and
>>> paste it into an email without trying to get to the bottom of it
>>> yourself.  At the very least, a Google search on, "AttributeError:
>>> 'NoneType' object has no attribute 'value'" will find you some answers.
>>>
>>> I've said it before, I'll say it again:  slow down.
>>>
>>> --Ned.
>>
>>
>> cookieID = cookie.get('ID').value
>>
>> is not returning what you said it will return
>>
>> and if cookie.get('ID') doenst exist it returns the error
>> AttributeError: 'NoneType' object has no attribute 'value'
>>
>> These are 2 problem.
>>
>> value aint being returned thw ehole Set-Cookie: ID=some_number is being
>> returned instead as tou cna see at http://superhost.gr/
>>
>> and the second problem is
>>
>> that if the cookie dosnt exist i get the error of: AttributeError:
>> 'NoneType' object has no attribute 'value'
>>
>> whne this line is tryign to be executed:
>> cookieID = cookie.get('ID').value
>>
>> How can i deal with thse 2 problems?
>>
> The best solution i cna think of is put the whole thing into a try: block
>
> try:
>     cookieID = cookie.get('ID').value
> except:
>     cookie['ID'] = random.randrange(0, 10000)
>     cookie['ID']['path'] = '/'
>     print( cookie )
>     cookieID = cookie['ID'].value
>
> print( '''Content-type: text/html; charset=utf-8\n''' )
>
> print( cookieID )
> sys.exit(0)
>
> That will avoid the NoneType errot but:
>
> that still print out:
> Set-Cookie: ID=7413
>
> instead of just the number
>

Nikos, you are now answering your own emails.  You are going too fast.  
Slow down, think through a solution before writing another email.  And 
seriously, consider IRC, you will be able to have a conversation with 
someone.  The email pace doesn't suit you.

A better solution is to check to see if you got None:

     if cookie.get('ID') is None:
         # make a new cookie....

--Ned.



More information about the Python-list mailing list