Select fails when cookie tried to get a numeric value

  • Thread starter Îίκος Αλεξόπουλος
  • Start date
Î

Îίκος Αλεξόπουλος

# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
vip = cookie.get('ID')

........
........

# if browser cookie does not exist, set it
vip = random.randrange(0, 10000)
cookie['ID'] = vip
cookie['ID']['path'] = '/'

# 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, vip, host, city, useros, browser, ref, lastvisit) )
=======================================
The above code i wrote gives me the following error:


[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] File
"/home/nikos/public_html/cgi-bin/metrites.py", line 209, in <module>
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]
cur.execute('''SELECT * FROM visitors WHERE counterID = %s and cookieID
= %s''', (cID, vip) )
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] File
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py",
line 100, in execute
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] escaped_args
= tuple(conn.escape(arg) for arg in args)
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] File
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py",
line 100, in <genexpr>
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] escaped_args
= tuple(conn.escape(arg) for arg in args)
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] File
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/connections.py",
line 650, in escape
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] return
escape_item(obj, self.charset)
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] File
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/converters.py",
line 31, in escape_item
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] encoder
= encoders[type(val)]
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] KeyError:
<class 'http.cookies.Morsel'>

What is the nature of the error?
<class 'http.cookies.Morsel'> ???

I'll iam trying to do is to give a cookie a random number.
 
N

Ned Batchelder

# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
vip = cookie.get('ID')

.......
.......

# if browser cookie does not exist, set it
vip = random.randrange(0, 10000)
cookie['ID'] = vip
cookie['ID']['path'] = '/'

# 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, vip, host, city, useros, browser, ref, lastvisit) )
=======================================
The above code i wrote gives me the following error:


[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] File
"/home/nikos/public_html/cgi-bin/metrites.py", line 209, in <module>
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]
cur.execute('''SELECT * FROM visitors WHERE counterID = %s and
cookieID = %s''', (cID, vip) )
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] File
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", line
100, in execute
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]
escaped_args = tuple(conn.escape(arg) for arg in args)
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] File
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py", line
100, in <genexpr>
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]
escaped_args = tuple(conn.escape(arg) for arg in args)
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] File
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/connections.py",
line 650, in escape
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] return
escape_item(obj, self.charset)
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] File
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/converters.py",
line 31, in escape_item
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] encoder =
encoders[type(val)]
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] KeyError:
<class 'http.cookies.Morsel'>

What is the nature of the error?
<class 'http.cookies.Morsel'> ???

I'll iam trying to do is to give a cookie a random number.

An important skill to master is reading the traceback for clues. It
shows you the lines of code that are involved in the failure. If you
read up the stack from the bottom, you can see that bottom four stack
frames are in the pymysql package. But the fifth frame is in your code,
at metrites.py, like 209, executing a select SQL statement. That's the
line to focus on.

You haven't shown us that code, but that is where the problem is.

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.

--Ned.
 
C

Chris Angelico

cur.execute('''INSERT INTO visitors (counterID, cookieID, host, city,
useros, browser, ref, lastvisit) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)''',
(cID, vip, host, city, useros, browser, ref, lastvisit) )
=======================================
The above code i wrote gives me the following error:


[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] File
"/home/nikos/public_html/cgi-bin/metrites.py", line 209, in <module>
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]
cur.execute('''SELECT * FROM visitors WHERE counterID = %s and cookieID=
%s''', (cID, vip) )

No, I don't think it does give you that error! Nikos, you've been
around this group a good while; why can you not learn to read an
exception traceback? Find line 209, which (as Ned said) is the one to
focus on, and look at it. If you can't figure it out yourself, at the
VERY least do us all a courtesy and show us the actual code that's
having the problem!

ChrisA
 
Î

Îίκος Αλεξόπουλος

Στις 5/10/2013 4:59 μμ, ο/η Chris Angelico έγÏαψε:
cur.execute('''INSERT INTO visitors (counterID, cookieID, host, city,
useros, browser, ref, lastvisit) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)''',
(cID, vip, host, city, useros, browser, ref, lastvisit) )
=======================================
The above code i wrote gives me the following error:


[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] File
"/home/nikos/public_html/cgi-bin/metrites.py", line 209, in <module>
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]
cur.execute('''SELECT * FROM visitors WHERE counterID = %s and cookieID =
%s''', (cID, vip) )

No, I don't think it does give you that error! Nikos, you've been
around this group a good while; why can you not learn to read an
exception traceback? Find line 209, which (as Ned said) is the one to
focus on, and look at it. If you can't figure it out yourself, at the
VERY least do us all a courtesy and show us the actual code that's
having the problem!

But i have given you the line that produces the error:

# 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:
message = "ΑΠΟ ΔΩ ΚΑΙ ΣΤΟ ΕΞΗΣ ΔΕΠΣΕ ΕΙΔΑ, ΔΕΠΣΕ ΞΕΡΩ, ΔΕΠΣΕ
ΑΚΟΥΣΑ! ΘΑ ΕΙΣΑΙ ΠΛΕΟΠΟ ΑΟΡΑΤΟΣ ΕΠΙΣΚΕΠΤΗΣ!!"
cookie['ID'] = random.randrange(0, 10000)
cookie['ID']['path'] = '/'
cookie['ID']['expires'] = 60*60*24*365 #this cookie will expire in a month



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?
 
C

Chris Angelico

But i have given you the line that produces the error:

The statement you quoted is an INSERT. The traceback quotes a SELECT.
These are not the same line of code. You still have not shown us the
actual line from the traceback.

ChrisA
 
Î

Îίκος Αλεξόπουλος

Στις 5/10/2013 5:24 μμ, ο/η Îίκος Αλεξόπουλος έγÏαψε:
Στις 5/10/2013 4:59 μμ, ο/η Chris Angelico έγÏαψε:
cur.execute('''INSERT INTO visitors (counterID, cookieID, host, city,
useros, browser, ref, lastvisit) VALUES (%s, %s, %s, %s, %s, %s, %s,
%s)''',
(cID, vip, host, city, useros, browser, ref, lastvisit) )
=======================================
The above code i wrote gives me the following error:


[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] File
"/home/nikos/public_html/cgi-bin/metrites.py", line 209, in <module>
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]
cur.execute('''SELECT * FROM visitors WHERE counterID = %s and
cookieID =
%s''', (cID, vip) )

No, I don't think it does give you that error! Nikos, you've been
around this group a good while; why can you not learn to read an
exception traceback? Find line 209, which (as Ned said) is the one to
focus on, and look at it. If you can't figure it out yourself, at the
VERY least do us all a courtesy and show us the actual code that's
having the problem!

But i have given you the line that produces the error:

# 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:
message = "ΑΠΟ ΔΩ ΚΑΙ ΣΤΟ ΕΞΗΣ ΔΕΠΣΕ ΕΙΔΑ, ΔΕΠΣΕ ΞΕΡΩ, ΔΕΠΣΕ
ΑΚΟΥΣΑ! ΘΑ ΕΙΣΑΙ ΠΛΕΟΠΟ ΑΟΡΑΤΟΣ ΕΠΙΣΚΕΠΤΗΣ!!"
cookie['ID'] = random.randrange(0, 10000)
cookie['ID']['path'] = '/'
cookie['ID']['expires'] = 60*60*24*365 #this cookie will expire
in a month



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?

The line is that is failign is any line of any insert update, sleect
staments that thats cookieID invilded like:

[Sat Oct 05 14:26:24 2013] [error] [client 108.162.229.114] File
"/home/nikos/public_html/cgi-bin/metrites.py", line 219, in <module>
[Sat Oct 05 14:26:24 2013] [error] [client 108.162.229.114]
cur.execute('''SELECT * FROM visitors WHERE counterID = %s and cookieID
= %s''', (cID, cookieID) )
 
Î

Îίκος Αλεξόπουλος

Στις 5/10/2013 5:28 μμ, ο/η Chris Angelico έγÏαψε:
The statement you quoted is an INSERT. The traceback quotes a SELECT.
These are not the same line of code. You still have not shown us the
actual line from the traceback.

ChrisA
Every mysql statemtns that involved cookieID fails.

in this example this:

# 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
 
B

Benjamin Rovny

# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
vip = cookie.get('ID')

.......
.......

# if browser cookie does not exist, set it
vip = random.randrange(0, 10000)
cookie['ID'] = vip
cookie['ID']['path'] = '/'

Problem here? Randrange returns an integer here, which you are then
treating like a dictionary (hence "Key Error"). That's the extent of my
knowledge though, I don't know about the "cookie" module.
# 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, vip, host, city, useros, browser, ref, lastvisit) )
=======================================
The above code i wrote gives me the following error:


[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] File
/home/nikos/public_html/cgi-bin/metrites.py" said:
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]
cur.execute('''SELECT * FROM visitors WHERE counterID = %s and cookieID =
%s''', (cID, vip) )
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] File
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py",
line 100, in execute
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] escaped_args
= tuple(conn.escape(arg) for arg in args)
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] File
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/cursors.py",
line 100 said:
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] escaped_args
= tuple(conn.escape(arg) for arg in args)
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] File
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/connections.py",
line 650, in escape
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] return escape_item(obj, self.charset)
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] File
"/usr/local/bin/python/lib/python3.3/site-packages/pymysql/converters.py",
line 31, in escape_item
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] encoder = encoders[type(val)]
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114] KeyError:
 
Î

Îίκος Αλεξόπουλος

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

Ned Batchelder

Στις 5/10/2013 5:28 μμ, ο/η Chris Angelico έγÏαψε:
Every mysql statemtns that involved cookieID fails.

in this example this:

# 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

Nikos, slow down. Don't post three emails before someone has a chance
to respond.

To get help, you have to show the code that goes along with the
traceback. Your subject line even says, "select fails", so you know it
is a select statement in the traceback. You have to show us *that
code*, and more than one line. You've shown the line here, but we don't
know what cID and cookieID are, so we can't help yet.

Saying "every mysql statement that involves cookieID fails" isn't
enough. Show us the code containing the line that actually is failing
in that traceback. Include enough of the code that we can figure out
what is going on.

You've said that you can do better here. Please try to.

--Ned.
 
Z

Zero Piraeus

:

Every mysql statemtns that involved cookieID fails.

in this example this:

# 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 every cur.execute() invocation that you try to pass cookieID to
fails, that suggests you can't pass cookieID to cur.execute() ...
perhaps because it's the wrong type.

The use of '%s' string interpolation suggests that cur.execute() is
expecting a string. Is cookieID a string? If not, is it some kind of
object that contains a string value within it? Maybe it has some kind of
attribute you could pass, like cookieID.value or similar?

That may or may not be correct, but it's the kind of mental process you
should be going through to solve your problem.

-[]z.
 
Î

Îίκος Αλεξόπουλος

Στις 5/10/2013 5:43 μμ, ο/η Ned Batchelder έγÏαψε:
Nikos, slow down. Don't post three emails before someone has a chance
to respond.

To get help, you have to show the code that goes along with the
traceback. Your subject line even says, "select fails", so you know it
is a select statement in the traceback. You have to show us *that
code*, and more than one line. You've shown the line here, but we don't
know what cID and cookieID are, so we can't help yet.

Saying "every mysql statement that involves cookieID fails" isn't
enough. Show us the code containing the line that actually is failing
in that traceback. Include enough of the code that we can figure out
what is going on.

You've said that you can do better here. Please try to.

--Ned.

Before we get to the part that iam actually try to insert, select
releveant records the mysql database we need to make sure that CookieID
is a number.


# 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?

If its not a number then it will not be selected or inserted properl
into/from MySQL.
 
N

Ned Batchelder

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

Nikos: stop sending so many emails. You've asked this question 3 times
now in this thread. You aren't even giving people a chance to respond.
If you want immediate feedback, use IRC. Email takes a little longer.

--Ned.
 
Z

Zero Piraeus

:

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"

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"

Please don't give identical or near-identical replies to multiple
messages in the thread; other members of the list are either reading all
of your posts or none of them, so repeating yourself like this is only
going to irritate whoever is reading.

Since printing cookieID doesn't produce the output you expect, the
obvious next step is to look up the documentation for whatever kind of
object it is. You can find out its type with

type(cookieID)

.... and then once you know that type (let's say for the sake of argument
it's a Biscuit object), find out about that type of object's attributes
either by googling for the docs or at the interpreter with

help(Biscuit)

As previously mentioned, there's likely to be some kind of 'value'
attribute that will return just the number you want.

-[]z.
 
N

Ned Batchelder

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

Îίκος Αλεξόπουλος

Στις 5/10/2013 6:12 μμ, ο/η Ned Batchelder έγÏαψε:
Στις 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.
 
A

Andreas Perstinger

Now i have it like this:

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

Just to be sure: That's not your real password, is it?

Bye, Andreas
 
Î

Îίκος Αλεξόπουλος

Στις 5/10/2013 6:06 μμ, ο/η Zero Piraeus έγÏαψε:
:



Please don't give identical or near-identical replies to multiple
messages in the thread; other members of the list are either reading all
of your posts or none of them, so repeating yourself like this is only
going to irritate whoever is reading.

Since printing cookieID doesn't produce the output you expect, the
obvious next step is to look up the documentation for whatever kind of
object it is. You can find out its type with

type(cookieID)

... and then once you know that type (let's say for the sake of argument
it's a Biscuit object), find out about that type of object's attributes
either by googling for the docs or at the interpreter with

help(Biscuit)

As previously mentioned, there's likely to be some kind of 'value'
attribute that will return just the number you want.
(e-mail address removed) [~/www/cgi-bin]# python
Python 3.3.2 (default, Aug 26 2013, 06:41:42)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'value'


And if you go to my webpage http://superhost.gr at the top corner you
see that allthough i use this code to get the value of the retrieved
cookie or set the value if ti do

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

# 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
print( cookie )
cookieID = cookie['ID'].value

print( '''Content-type: text/html; charset=utf-8\n''' )

print( cookieID )
sys.exit(0)

The output is: Set-Cookie: ID=1376

But how is this possible since we applied the .value attribute in the
cookie?
 
Î

Îίκος Αλεξόπουλος

Στις 5/10/2013 6:12 μμ, ο/η Ned Batchelder έγÏαψε:
Στις 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'
 
N

Ned Batchelder

Στις 5/10/2013 6:12 μμ, ο/η Ned Batchelder έγÏαψε:
Στις 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,757
Messages
2,569,543
Members
45,026
Latest member
camilin05

Latest Threads

Top