Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #56183 > unrolled thread
| Started by | Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> |
|---|---|
| First post | 2013-10-05 16:38 +0300 |
| Last post | 2013-10-06 22:16 -0400 |
| Articles | 13 on this page of 33 — 10 participants |
Back to article view | Back to comp.lang.python
Select fails when cookie tried to get a numeric value Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-10-05 16:38 +0300
Re: Select fails when cookie tried to get a numeric value Ned Batchelder <ned@nedbatchelder.com> - 2013-10-05 09:53 -0400
Re: Select fails when cookie tried to get a numeric value Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-10-05 17:40 +0300
Re: Select fails when cookie tried to get a numeric value Ned Batchelder <ned@nedbatchelder.com> - 2013-10-05 10:54 -0400
Re: Select fails when cookie tried to get a numeric value Ned Batchelder <ned@nedbatchelder.com> - 2013-10-05 11:12 -0400
Re: Select fails when cookie tried to get a numeric value Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-10-05 18:31 +0300
Re: Select fails when cookie tried to get a numeric value Andreas Perstinger <andipersti@gmail.com> - 2013-10-05 17:47 +0200
Re: Select fails when cookie tried to get a numeric value Chris Angelico <rosuav@gmail.com> - 2013-10-06 09:36 +1100
Re: Select fails when cookie tried to get a numeric value Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-10-05 18:52 +0300
Re: Select fails when cookie tried to get a numeric value Ned Batchelder <ned@nedbatchelder.com> - 2013-10-05 12:08 -0400
Re: Select fails when cookie tried to get a numeric value Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-10-05 19:14 +0300
Re: Select fails when cookie tried to get a numeric value Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-10-05 19:17 +0300
Re: Select fails when cookie tried to get a numeric value Ned Batchelder <ned@nedbatchelder.com> - 2013-10-05 12:42 -0400
Re: Select fails when cookie tried to get a numeric value Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-10-05 19:53 +0300
Re: Select fails when cookie tried to get a numeric value Zero Piraeus <z@etiol.net> - 2013-10-05 12:06 -0300
Re: Select fails when cookie tried to get a numeric value Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-10-05 18:50 +0300
Re: Select fails when cookie tried to get a numeric value Chris Angelico <rosuav@gmail.com> - 2013-10-05 23:59 +1000
Re: Select fails when cookie tried to get a numeric value Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-10-05 17:24 +0300
Re: Select fails when cookie tried to get a numeric value Chris Angelico <rosuav@gmail.com> - 2013-10-06 00:28 +1000
Re: Select fails when cookie tried to get a numeric value Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-10-05 17:30 +0300
Re: Select fails when cookie tried to get a numeric value Ned Batchelder <ned@nedbatchelder.com> - 2013-10-05 10:43 -0400
Re: Select fails when cookie tried to get a numeric value Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-10-05 17:47 +0300
Re: Select fails when cookie tried to get a numeric value Zero Piraeus <z@etiol.net> - 2013-10-05 11:44 -0300
Re: Select fails when cookie tried to get a numeric value MRAB <python@mrabarnett.plus.com> - 2013-10-05 17:51 +0100
Re: Select fails when cookie tried to get a numeric value Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-10-05 17:28 +0300
Re: Select fails when cookie tried to get a numeric value Andreas Perstinger <andipersti@gmail.com> - 2013-10-05 18:56 +0200
Re: Select fails when cookie tried to get a numeric value Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-10-05 20:38 +0300
Re: Select fails when cookie tried to get a numeric value Benjamin Rovny <rdr.vladimir@gmail.com> - 2013-10-05 09:35 -0500
Re: Select fails when cookie tried to get a numeric value Terry Reedy <tjreedy@udel.edu> - 2013-10-05 18:06 -0400
Re: Select fails when cookie tried to get a numeric value Denis McMahon <denismfmcmahon@gmail.com> - 2013-10-05 23:36 +0000
Re: Select fails when cookie tried to get a numeric value Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-10-06 08:20 +0300
Re: Select fails when cookie tried to get a numeric value Denis McMahon <denismfmcmahon@gmail.com> - 2013-10-06 14:57 +0000
Re: Select fails when cookie tried to get a numeric value Piet van Oostrum <piet@vanoostrum.org> - 2013-10-06 22:16 -0400
Page 2 of 2 — ← Prev page 1 [2]
| From | Ned Batchelder <ned@nedbatchelder.com> |
|---|---|
| Date | 2013-10-05 10:43 -0400 |
| Message-ID | <mailman.746.1380984241.18130.python-list@python.org> |
| In reply to | #56189 |
On 10/5/13 10:30 AM, Νίκος Αλεξόπουλος wrote:
> Στις 5/10/2013 5:28 μμ, ο/η Chris Angelico έγραψε:
>> On Sun, Oct 6, 2013 at 12:24 AM, Νίκος Αλεξόπουλος
>> <nikos.gr33k@gmail.com> wrote:
>>> 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
>>
> 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.
[toc] | [prev] | [next] | [standalone]
| From | Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-10-05 17:47 +0300 |
| Message-ID | <l2p8qo$j06$5@dont-email.me> |
| In reply to | #56191 |
Στις 5/10/2013 5:43 μμ, ο/η Ned Batchelder έγραψε:
> On 10/5/13 10:30 AM, Νίκος Αλεξόπουλος wrote:
>> Στις 5/10/2013 5:28 μμ, ο/η Chris Angelico έγραψε:
>>> On Sun, Oct 6, 2013 at 12:24 AM, Νίκος Αλεξόπουλος
>>> <nikos.gr33k@gmail.com> wrote:
>>>> 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
>>>
>> 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.
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.
[toc] | [prev] | [next] | [standalone]
| From | Zero Piraeus <z@etiol.net> |
|---|---|
| Date | 2013-10-05 11:44 -0300 |
| Message-ID | <mailman.752.1380991203.18130.python-list@python.org> |
| In reply to | #56189 |
:
On Sat, Oct 05, 2013 at 05:30:53PM +0300, Νίκος Αλεξόπουλος wrote:
> 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.
--
Zero Piraeus: inter caetera
http://etiol.net/pubkey.asc
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2013-10-05 17:51 +0100 |
| Message-ID | <mailman.755.1380991878.18130.python-list@python.org> |
| In reply to | #56189 |
On 05/10/2013 15:44, Zero Piraeus wrote:
> On Sat, Oct 05, 2013 at 05:30:53PM +0300, Νίκος Αλεξόπουλος wrote:
>> 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?
>
MySQL string interpolation uses "%s" as the placeholder, but the value
doesn't have to be a string.
> That may or may not be correct, but it's the kind of mental process
> you should be going through to solve your problem.
>
[toc] | [prev] | [next] | [standalone]
| From | Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-10-05 17:28 +0300 |
| Message-ID | <l2p7mt$j06$2@dont-email.me> |
| In reply to | #56186 |
Στις 5/10/2013 5:24 μμ, ο/η Νίκος Αλεξόπουλος έγραψε:
> Στις 5/10/2013 4:59 μμ, ο/η Chris Angelico έγραψε:
>> On Sat, Oct 5, 2013 at 11:38 PM, Νίκος Αλεξόπουλος
>> <nikos.gr33k@gmail.com> wrote:
>>> 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) )
--
What is now proved was at first only imagined! & WebHost
<http://superhost.gr>
[toc] | [prev] | [next] | [standalone]
| From | Andreas Perstinger <andipersti@gmail.com> |
|---|---|
| Date | 2013-10-05 18:56 +0200 |
| Message-ID | <mailman.756.1380992210.18130.python-list@python.org> |
| In reply to | #56186 |
On 05.10.2013 16:24, Νίκος Αλεξόπουλος wrote:
> # initialize cookie
> cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
> cookie.load( cookie )
Watch:
>>> cookie1 = cookies.SimpleCookie('ID=42')
>>> cookie1.load(cookie1)
>>> print(cookie1)
Set-Cookie: ID="Set-Cookie: ID=42"
>>> cookie1.get('ID').value
'Set-Cookie: ID=42'
And now watch this:
>>> cookie2 = cookies.SimpleCookie('ID=42')
>>> print(cookie2)
Set-Cookie: ID=42
>>> cookie2.get('ID').value
'42'
Explanation:
http://docs.python.org/3/library/http.cookies.html#http.cookies.BaseCookie.load
>>> c = cookies.SimpleCookie('ID=42')
>>> isinstance(c, dict)
True
>>> c.items()
dict_items([('ID', <Morsel: ID='42'>)])
Bye, Andreas
[toc] | [prev] | [next] | [standalone]
| From | Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-10-05 20:38 +0300 |
| Message-ID | <l2piqi$k0$1@dont-email.me> |
| In reply to | #56210 |
Στις 5/10/2013 7:56 μμ, ο/η Andreas Perstinger έγραψε:
> On 05.10.2013 16:24, Νίκος Αλεξόπουλος wrote:
>> # initialize cookie
>> cookie = cookies.SimpleCookie( os.environ.get('HTTP_COOKIE') )
>> cookie.load( cookie )
>
> Watch:
>
> >>> cookie1 = cookies.SimpleCookie('ID=42')
> >>> cookie1.load(cookie1)
> >>> print(cookie1)
> Set-Cookie: ID="Set-Cookie: ID=42"
> >>> cookie1.get('ID').value
> 'Set-Cookie: ID=42'
>
> And now watch this:
>
> >>> cookie2 = cookies.SimpleCookie('ID=42')
> >>> print(cookie2)
> Set-Cookie: ID=42
> >>> cookie2.get('ID').value
> '42'
>
> Explanation:
>
> http://docs.python.org/3/library/http.cookies.html#http.cookies.BaseCookie.load
>
>
> >>> c = cookies.SimpleCookie('ID=42')
> >>> isinstance(c, dict)
> True
> >>> c.items()
> 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.
--
What is now proved was at first only imagined! & WebHost
<http://superhost.gr>
[toc] | [prev] | [next] | [standalone]
| From | Benjamin Rovny <rdr.vladimir@gmail.com> |
|---|---|
| Date | 2013-10-05 09:35 -0500 |
| Message-ID | <mailman.759.1381005595.18130.python-list@python.org> |
| In reply to | #56183 |
[Multipart message — attachments visible in raw view] — view raw
On Oct 5, 2013 8:42 AM, "Νίκος Αλεξόπουλος" <nikos.gr33k@gmail.com> wrote:
>
> # 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", 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.
> --
> https://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2013-10-05 18:06 -0400 |
| Message-ID | <mailman.762.1381010831.18130.python-list@python.org> |
| In reply to | #56183 |
On 10/5/2013 9:38 AM, Νίκος Αλεξόπουλος wrote:
> # 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.
--
Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2013-10-05 23:36 +0000 |
| Message-ID | <l2q7pe$2o4$2@dont-email.me> |
| In reply to | #56183 |
On Sat, 05 Oct 2013 16:38:14 +0300, Νίκος Αλεξόπουλος wrote: > [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! -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-10-06 08:20 +0300 |
| Message-ID | <l2qrv1$4lf$1@dont-email.me> |
| In reply to | #56226 |
Στις 6/10/2013 2:36 πμ, ο/η Denis McMahon έγραψε: > On Sat, 05 Oct 2013 16:38:14 +0300, Νίκος Αλεξόπουλος wrote: > >> [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? -- What is now proved was at first only imagined! & WebHost <http://superhost.gr>
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2013-10-06 14:57 +0000 |
| Message-ID | <l2rtpa$4sf$1@dont-email.me> |
| In reply to | #56237 |
On Sun, 06 Oct 2013 08:20:35 +0300, Νίκος Αλεξόπουλος wrote: > 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. -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Piet van Oostrum <piet@vanoostrum.org> |
|---|---|
| Date | 2013-10-06 22:16 -0400 |
| Message-ID | <m2mwmlzuy6.fsf@cochabamba.vanoostrum.org> |
| In reply to | #56237 |
Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> writes: > 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. -- Piet van Oostrum <piet@vanoostrum.org> WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4]
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.python
csiph-web