本文介绍了如何在Flask中将unicode标头转换为字节字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Flask应用程序,已经能够在开发服务器上运行.但是,当我尝试在mod_wsgi下运行相同的应用程序时,出现错误:

I have a flask app that I've been able to get running on my development server. However, when I try to run the same app under mod_wsgi I get an error:

TypeError: expected byte string object for header name, value of type unicode found

我尝试过多种不同的转换标头的方式,但是我遇到了相同的错误:

I've tried to convert the headers many different ways but I'm getting the same error:

for k,v in dict(request.headers).iteritems():
    response.headers[k.encode('latin-1')] = v.encode('latin-1')

我也尝试了以下方法,但得到了相同的确切错误:

I've also tried the following but get the same exact error:

.encode('utf-8'), decode('utf-8'), decode('latin-1'), str()

我做错什么了吗?

编辑(真正的堆栈跟踪...我认为):

EDIT (the real stacktrace...I think):

[Wed Mar 12 23:26:49 2014] [notice] Apache/2.2.25 (Unix) mod_wsgi/3.4 Python/2.7.5 configured -- resuming normal operations
[Wed Mar 12 23:26:49 2014] [info] Server built: Sep  9 2013 06:59:27
[Wed Mar 12 23:26:49 2014] [info] mod_wsgi (pid=524834): Starting process 'api' with threads=1.
[Wed Mar 12 23:26:49 2014] [info] mod_wsgi (pid=524834): Initializing Python.
[Wed Mar 12 23:26:49 2014] [info] mod_wsgi (pid=524835): Starting process 'api' with threads=1.
[Wed Mar 12 23:26:49 2014] [info] mod_wsgi (pid=524835): Initializing Python.
[Wed Mar 12 23:26:49 2014] [info] mod_wsgi (pid=524833): Starting process 'api' with threads=1.
[Wed Mar 12 23:26:49 2014] [info] mod_wsgi (pid=524833): Initializing Python.
[Wed Mar 12 23:26:49 2014] [info] mod_wsgi (pid=524832): Starting process 'api' with threads=1.
[Wed Mar 12 23:26:49 2014] [info] mod_wsgi (pid=524832): Initializing Python.
[Wed Mar 12 23:26:49 2014] [info] mod_wsgi (pid=524831): Starting process 'api' with threads=1.
[Wed Mar 12 23:26:49 2014] [info] mod_wsgi (pid=524831): Initializing Python.
[Wed Mar 12 23:26:49 2014] [info] mod_wsgi (pid=524833): Attach interpreter ''.
[Wed Mar 12 23:26:49 2014] [info] mod_wsgi (pid=524833): Adding '/path/to/api/lib/python2.7' to path.
[Wed Mar 12 23:26:49 2014] [info] mod_wsgi (pid=524834): Attach interpreter ''.
[Wed Mar 12 23:26:49 2014] [info] mod_wsgi (pid=524834): Adding '/path/to/api/lib/python2.7' to path.
[Wed Mar 12 23:26:49 2014] [info] mod_wsgi (pid=524832): Attach interpreter ''.
[Wed Mar 12 23:26:49 2014] [info] mod_wsgi (pid=524832): Adding '/path/to/api/lib/python2.7' to path.
[Wed Mar 12 23:26:49 2014] [info] mod_wsgi (pid=524835): Attach interpreter ''.
[Wed Mar 12 23:26:49 2014] [info] mod_wsgi (pid=524835): Adding '/path/to/api/lib/python2.7' to path.
[Wed Mar 12 23:26:49 2014] [info] mod_wsgi (pid=524831): Attach interpreter ''.
[Wed Mar 12 23:26:49 2014] [info] mod_wsgi (pid=524831): Adding '/path/to/api/lib/python2.7' to path.
[Wed Mar 12 23:26:51 2014] [info] mod_wsgi (pid=524833): Create interpreter 'web23.webfaction.com|'.
[Wed Mar 12 23:26:51 2014] [info] mod_wsgi (pid=524833): Adding '/path/to/api/lib/python2.7' to path.
[Wed Mar 12 23:26:51 2014] [info] [client 127.0.0.1] mod_wsgi (pid=524833, process='api', application='web23.webfaction.com|'): Loading WSGI script '/path/to/api/wsgi.py'.
[Wed Mar 12 23:26:52 2014] [error] [client 127.0.0.1] mod_wsgi (pid=524833): Exception occurred processing WSGI script '/path/to/api/wsgi.py'.
[Wed Mar 12 23:26:52 2014] [error] [client 127.0.0.1] TypeError: expected byte string object for header name, value of type unicode found

编辑#2:这是Flask中我试图获取令牌的视图,以及我如何修改标头(或至少尝试这样做):

EDIT #2:Here's the view in Flask where I'm trying to get a token and how I'm modifying the headers (or, at least, trying to):

@app.route('/oauth/token/', methods=['GET','POST'],subdomain='api')
@oauth.token_handler # e.g. the decorator for OAuthlib/OAuthlib-Flask
def access_token():
  return None

@app.after_request
def after(response):
  import urllib
  for k,v in dict(request.headers).iteritems():
    response.headers[urllib.quote(k)] = urllib.quote(v)

  return response

编辑#3("print dict(request.headers).items()"的输出):

EDIT #3 ( output from "print dict(request.headers).items()" ):

[('Forwarded-Request-Uri', u'/oauth/token?client_secret=rDRzy2Qgt627dZK6eFtnylluOad7PRuLNUBothIpb0KQWJmOBl&grant_type=client_credentials&client_id=lryd8PqzlSObOQNUUEgEjqKYIeCnnkifs1s16NZ1'), ('Accept-Encoding', u'gzip, deflate, compress'), ('X-Forwarded-Host', u'api.example.com'), ('X-Forwarded-For', u'917.85.235.27'), ('Http-X-Forwarded-Proto', u'https'), ('Host', u'api.example.com'), ('Accept', u'*/*'), ('User-Agent', u'python-requests/2.2.1 CPython/2.7.5+ Linux/3.11.0-12-generic'), ('Connection', u'close'), ('X-Forwarded-Proto', u'https'), ('Https', u'on'), ('X-Forwarded-Server', u'api.example.com'), ('X-Forwarded-Ssl', u'on')]

推荐答案

表现如何

bytes(whatever_unicode.encode("utf-8"))

或根据J.F. Sebastians的评论

or per J.F. Sebastians comment

some_unicode.encode("ISO-8859-1")

或者也许

import urllib
urllib.quote(unicode_string)

其中一个应该起作用...我认为

one of those should work ... I think

这篇关于如何在Flask中将unicode标头转换为字节字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:38