Select fails when cookie tried to get a numeric value

Νίκος Αλεξόπουλος nikos.gr33k at gmail.com
Sat Oct 5 11:31:42 EDT 2013


Στις 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.

Thank you Ned, indeed '.value' needed to just print the number.
Now i have it like this:

# connect to database
con = pymysql.connect( db = 'nikos_metrites', user = 'nikos_root', 
passwd = 't1abhp2r!', charset = 'utf8', host = 'localhost' )
cur = con.cursor()

# initialize cookie and retrieve cookie from clients broswer
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )

# if browser cookie does not exist, set it
if not cookie.get('ID'):
	cookie['ID'] = random.randrange(0, 10000)
	cookie['ID']['path'] = '/'
	cookie['ID']['expires'] = 60*60*24*365		#this cookie will expire in a month
	print( cookie )
	cookieID = cookie['ID'].value
# if browser cookie exist, just retrieve it
else:
	cookieID = cookie.get('ID').value

and it does not output an error, but for some reason the inserting and 
selecting never happens.

here si the releveant code:


if cookieID != 1977 and re.search( 
r'(msn|gator|amazon|yandex|reverse|who|cloudflare|fetch|barracuda|spider|google|crawl|pingdom)', 
host ) is None:

try:
		# find the visitor record for the (saved) cID and current host
		cur.execute('''SELECT * FROM visitors WHERE counterID = %s and 
cookieID = %s''', (cID, cookieID) )
		data = cur.fetchone()        #cookieID is unique
			
		if not data:
			# first time visitor on this page, create new record
			cur.execute('''INSERT INTO visitors (counterID, cookieID, host, city, 
useros, browser, ref, lastvisit) VALUES (%s, %s, %s, %s, %s, %s, %s, 
%s)''', (cID, cookieID, host, city, useros, browser, ref, lastvisit) )
		else:
			# found the page, save its primary key for later use
			vID = data[0]
			# UPDATE record using retrieved vID
			cur.execute('''UPDATE visitors SET host = %s, city = %s, useros = %s, 
browser = %s, ref= %s, hits = hits + 1, lastvisit = %s
									WHERE counterID = %s and cookieID = %s''', (host, city, useros, 
browser, ref, lastvisit, vID, cookieID) )
		
	except pymysql.ProgrammingError as e:
		print( repr(e) )
		sys.exit(0)
=====================


Any ideas as to what i shoudld try?
the statemnt don't return any error back though and the cookieID is 
indeed now a proper number so i see no reason as to why they fail.

-- 
What is now proved was at first only imagined! & WebHost
<http://superhost.gr>



More information about the Python-list mailing list