Select fails when cookie tried to get a numeric value

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

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

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

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

Στις 5/10/2013 7:14 μμ, ο/η Îίκος Αλεξόπουλος έγÏαψε:
Στις 5/10/2013 7:08 μμ, ο/η Ned Batchelder έγÏαψε:
Στις 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
 
N

Ned Batchelder

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

MRAB

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?
MySQL string interpolation uses "%s" as the placeholder, but the value
doesn't have to be a string.
 
Î

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

Στις 5/10/2013 7:42 μμ, ο/η Ned Batchelder έγÏαψε:
A better solution is to check to see if you got None:

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

I have tried everythign even wgat you suggested right now:
here is is:

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

if cookie.get('ID') is not None:
cookieID = cookie['ID'].value
else:
x = random.randrange(0, 10000)
cookie['ID'] = x
cookie['ID']['path'] = '/'
print( cookie )
cookieID = x

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

For some reason althogh the cookie does exist in my browser the returnd
value of cookieI D = cookie['ID'].value is always: Set-Cookie: ID=7482

instead of just the number.
But why? isnt value to return just the number?

You can see the result of the above code yourself at the top left when
you visit: http://superhost.gr

it always retursn the whole string instead of just the number...

why can we isolate th damn number only?
in the else i manages to isolate it but not when i try to retrive it.
 
Î

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

Στις 5/10/2013 7:56 μμ, ο/η Andreas Perstinger έγÏαψε:
# initialize cookie
cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
cookie.load( cookie )
Watch:
'Set-Cookie: ID=42'

And now watch this:
dict_items([('ID', <Morsel: ID='42'>)])

Bye, Andreas

Thank you very much Andreas,

it was this strnage behaviour that got me stuch for hours.
Now value gets returned properly.
 
T

Terry Reedy

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

The prefix added to every line of the traceback by the logging process
[Sat Oct 05 13:33:24 2013] [error] [client 108.162.229.114]
makes the traceback much harder to read. I suggest removing it before
posting. This is easy with a global replace with nothing. If you control
the logging and can have the prefix printed just once, even better.
 
C

Chris Angelico

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

Do you understand the expression "race condition", and how it applies
to the above code? If not, you MUST read up on that before relying on
code like this, and as that's not a Python question, I recommend
Google and Wikipedia.[1]

ChrisA

[1] Yes, I know they're not primary sources. For something like this,
tertiary sources are going to do him fine.
 
D

Denis McMahon

[my cookie code is fucked]

Hi Nick

I had never used python for www before yesterday. I had never used python
before about 6 months ago.

In the last 24 hours I have installed mod-wsgi on my apache web server,
and written test code that enables me to see the result of all
environment data, parse the get string, parse post data, set and parse
cookies, and save session data to a file specific to the cookie data.

There may be better ways to do some of it, but it works.

Here is a link to the text of my python file:

http://www.sined.co.uk/tmp/index.py.txt

Once you have read through it (there are comments) and understood how it
handles cookies and session data, you will realise that you can add and
modify any session data just by adding relevant items to and reading them
from the session data dictionary. The session data stays on the server,
the cookie simply identifies the session (and the session data file), and
hopefully is unique to the user.

Don't ask me any questions about why I did something the way I did it. I
did it that way because it works. If anyone knows a different way that
also works and maybe works better, feel free to discuss it. But Nick,
don't you dare suggest any change that doesn't work because you think it
looks better.

If you don't understand a module function that I've used, read the python
documentation for it.

If you think you have a question that is not covered by the above
statements, feel free to ask me, here, why I wrote a given line number or
group of line numbers the way I did. However, see the notes above - the
answer will probably be "because it works that way".

If you quote the whole file or even large chunks of it here, I am
finished with trying to help you, ever!
 
Î

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

Στις 6/10/2013 2:36 πμ, ο/η Denis McMahon έγÏαψε:
[my cookie code is fucked]

Hi Nick

I had never used python for www before yesterday. I had never used python
before about 6 months ago.

In the last 24 hours I have installed mod-wsgi on my apache web server,
and written test code that enables me to see the result of all
environment data, parse the get string, parse post data, set and parse
cookies, and save session data to a file specific to the cookie data.

There may be better ways to do some of it, but it works.

Here is a link to the text of my python file:

http://www.sined.co.uk/tmp/index.py.txt

Once you have read through it (there are comments) and understood how it
handles cookies and session data, you will realise that you can add and
modify any session data just by adding relevant items to and reading them
from the session data dictionary. The session data stays on the server,
the cookie simply identifies the session (and the session data file), and
hopefully is unique to the user.

Don't ask me any questions about why I did something the way I did it. I
did it that way because it works. If anyone knows a different way that
also works and maybe works better, feel free to discuss it. But Nick,
don't you dare suggest any change that doesn't work because you think it
looks better.

If you don't understand a module function that I've used, read the python
documentation for it.

If you think you have a question that is not covered by the above
statements, feel free to ask me, here, why I wrote a given line number or
group of line numbers the way I did. However, see the notes above - the
answer will probably be "because it works that way".

If you quote the whole file or even large chunks of it here, I am
finished with trying to help you, ever!

Thank you Denis, i didn't knew about sessions up until i saw you post.
Your code is very advanced for me to read but i will try to "decode it"
I though that if we want to read something from our visitor, something
we want, we have to save it to his browser a cookie, that we later
retrieve within our python script's code.

What "sessions" do more compared to just using cookies?

i use 'cgi', but i noticed you used 'mod-wsgi'.
I want to ask you why you chose the latter? Is the latter better than
the former?

Is it faster? Does it bahave the same way?
I my code works works with cgi, which it does, will the same code works
with mod-wcgi?
 
D

Denis McMahon

Thank you Denis, i didn't knew about sessions up until i saw you post.
Your code is very advanced for me to read but i will try to "decode it"
I though that if we want to read something from our visitor, something
we want, we have to save it to his browser a cookie, that we later
retrieve within our python script's code.

What "sessions" do more compared to just using cookies?

The concept of a session is that you preserve data in the server between
multiple page requests from the same client.

You do this by issuing a session id to the client in a cookie, and using
the session id as a key to identify that user's data in the server.

I use the session id as a filename in the tmp files dir on the server,
and in the file I store a pickled dictionary that contains all the
session data for the user. It should be just as easy to do this using a
database with three fields, the session id, the last used time, and a
binary object (or a string) to hold the pickled data. You could use json
instead of pickle, that just creates a string.

The big advantage of a session is that you keep the data on the server,
and only send the session id to the browser. This means that the user can
not manipulate the session data.

If you don't understand why this is important, you need to stop coding
websites until you do understand why this is important. As the importance
of this is not a python issue, it is not appropriate to discuss here.
i use 'cgi', but i noticed you used 'mod-wsgi'.
I want to ask you why you chose the latter? Is the latter better than
the former?

It was a choice between mod-python and mod-wsgi. I chose mod-wsgi. I
think I felt that it was better supported in apache than mod-python, and
it seemed to be better thought of in various forums.
Is it faster? Does it bahave the same way?

I have no idea. I prefer using specific apache modules for server side
scripting where possible, rather than cgi, as I find that they are
generally less cpu and memory intensive than using the cgi interface.

Although my compiled fortran, ada, basic, pascal and c modules still use
mod-cgi.[1]
I my code works works with cgi, which it does, will the same code works
with mod-wcgi?

I have no idea. Also, what is mod-wcgi anyway?

[1] I got bored one weekend and decided to write basic form handling in
several of the languages I knew just to prove I could do it.
 
P

Piet van Oostrum

Îίκος Αλεξόπουλος said:
i use 'cgi', but i noticed you used 'mod-wsgi'.
I want to ask you why you chose the latter? Is the latter better than
the former?

Is it faster? Does it bahave the same way?
I my code works works with cgi, which it does, will the same code works
with mod-wcgi?

You probably mean mod-wsgi. It is faster than CGI because it doesn't have to start a new Python interpreter for each request. And the protocol it uses is different so you should rewrite your scripts if you want to use WSGI. There are bridges to use your CGI script within WSGI, but it needs care; you cannot use it for every CGI script - your script has to work in a certain way (like being careful with initializing everything everytime it runs). That said, you can use both at the same time in a web server: some CGI scripts and some WSGI, and convert them one after one if you think it is necessary. For example a script that is seldom used can stay as CGI script.

And last but not least: not every Apache installation has mod-wsgi enabled. I think most hosting providers don't.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top