Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #66434 > unrolled thread
| Started by | Makoto Kuwata <kwa@kuwata-lab.com> |
|---|---|
| First post | 2014-02-15 13:39 +0900 |
| Last post | 2014-02-15 13:39 +0900 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
[Q] Beaker 1.6.4 not work on Python3 Makoto Kuwata <kwa@kuwata-lab.com> - 2014-02-15 13:39 +0900
| From | Makoto Kuwata <kwa@kuwata-lab.com> |
|---|---|
| Date | 2014-02-15 13:39 +0900 |
| Subject | [Q] Beaker 1.6.4 not work on Python3 |
| Message-ID | <mailman.6994.1392471063.18130.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
Hi,
Does Beaker 1.6.4 work on Python3 ?
Is there anyone using Beaker on Python3?
I got the following error on Python 3.3:
File "/opt/lang/python/3.2.2/lib/python3.2/http/cookies.py", line 486, in
__setitem__
rval, cval = self.value_encode(value)
File
"/opt/lang/python/3.2.2/lib/python3.2/site-packages/Beaker-1.6.4-py3.2.egg/beaker/session.py",
line 70, in value_encode
sig = HMAC.new(self.secret, val.encode('UTF-8'), SHA1).hexdigest()
AttributeError: 'bytes' object has no attribute 'encode'
The following is a monkey patch to avoid this error,
but I'm not sure that it is correct solution.
from beaker.crypto import hmac as HMAC, hmac_sha1 as SHA1
from beaker.session import SignedCookie
def value_encode(self, val):
#sig = HMAC.new(self.secret, val.encode('UTF-8'), SHA1).hexdigest()
sig = HMAC.new(self.secret, val, SHA1).hexdigest()
return str(val), ("%s%s" % (sig, val))
SignedCookie.value_encode = value_encode
And, even with monkey patching, Beaker's SessionMiddleware
doesn't save session correctly on Python3.
Please help me: I want to run Beaker 1.6.4 on Python 3.
(Pyton 3.3.3, MacOSX)
Here is my sample code (which works on Python2.7 very well!):
------------------------------------------------------
# -*- coding: utf-8 -*-
import sys
import waitress
from beaker.middleware import SessionMiddleware
def testapp(environ, start_response):
session = environ.get('beaker.session')
count = session.get('count', 0) + 1
session['count'] = count
session.save()
content = "count=%s" % count
#
start_response('200 OK', [('Content-Type', 'text/plain')])
return [content.encode('utf-8')]
config = {
'session.type': 'cookie',
'session.validate_key': 'mysecretstring',
}
app = SessionMiddleware(testapp, config=config)
## monkey patch for Python3
python3 = sys.version_info[0] == 3
if 0 and python3:
from beaker.crypto import hmac as HMAC, hmac_sha1 as SHA1
from beaker.session import SignedCookie
def value_encode(self, val):
#sig = HMAC.new(self.secret, val.encode('UTF-8'), SHA1).hexdigest()
sig = HMAC.new(self.secret, val, SHA1).hexdigest()
return str(val), ("%s%s" % (sig, val))
SignedCookie.value_encode = value_encode
## ----
waitress.serve(app, port=8080)
------------------------------------------------------
--
regards,
makoto kuwata
Back to top | Article view | comp.lang.python
csiph-web