Flup, AuthKit, & unicode strings


If you’re on a hosting environment (e.g., bluehost.com) where the only option to run Python/Pylons is via flup (fast cgi), then you may have experienced this error:


<'exceptions.AssertionError'>: write() argument must be string

The bottom line is that the WSGI spec only supports plain strings.

AuthKit, is a middleware that intercepts HTTP request/response in order to enforce authentication and authorization. One issue that you will encounter is that any page that gets rendered via AuthKit by default will be in unicode. This causes flup to throw the above exception. The fix is pretty straight forward:

def render_signin():
    result = render('/signin.mako')
    result = result.replace('%', '%%').replace('FORM_ACTION', '%s')
    return unicode(result).encode('utf8')

Simply cast the result to unicode and encode it to utf8.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.