[Q] Beaker 1.6.4 not work on Python3
Makoto Kuwata
kwa at kuwata-lab.com
Fri Feb 14 23:39:14 EST 2014
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140215/5bc96331/attachment.html>
More information about the Python-list
mailing list