SAE Python urllib2引发异常问题的关键点:HTTP(S)请求包含headers,且headers的键名为unicode

下面是引发异常的代码:

pythonimport urllib2
headers = {u\'Authorization\': u\'Bearer 83F40E96FB6882686F4DF1E17105D04E\'}
req = urllib2.Request(\'https://github.com\', headers=headers)
urllib2.urlopen(req).read()

代码执行时会得到类似如下的报错信息(错误是在请求发起前引发的):

textHTTPError: HTTP Error 400: Bad request

下面就上面的请求做假设分析:

  1. 若请求不包含headers,则请求正常;
  2. 若请求包含headers,且headers字典键名不为unicode类型,则请求正常;
  3. 若请求包含headers,且headers字典键名为unicode类型,则请求前必然会引发HTTP Error 400: Bad request错误。

上面这段代码,我在本地机器、VPS服务器、其它的应用引擎上测试都是正常的,Python版本是2.6和2.7。因此,有很大的可能是SAE Pyhton环境的问题。这个问题已经在SAE工单系统提交了,期待会有好结果吧~

在某些应用场景下,使用了某些第三方的库(比如Flask-OAuthlib),有可能headers的键名是unicode类型的,那就必须进行转换。下面是在stackoverflow上找到的一个转换函数:

pythondef convert_keys_to_string(dictionary):
    \"\"\"Recursively converts dictionary keys to strings.\"\"\"
    if not isinstance(dictionary, dict):
        return dictionary
    return dict((str(k), convert_keys_to_string(v)) 
        for k, v in dictionary.items())

参考资料:

  • http://stackoverflow.com/questions/1254454/fastest-way-to-convert-a-dicts-keys-values-from-unicode-to-str