UnicodeDecodeError:'ascii'编解码器不能用urllib2 urlopen()解码字节XX

时间:2011-12-09 22:05:33

标签: python urllib2 django-nonrel

我正在尝试将一些数据发布到网址,但我正在运行一个对我没有任何意义的python错误。

这是错误:

Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/logging/__init__.py", line 765, in emit
UnicodeDecodeError: 'ascii' codec can't decode byte 0x83 in position 1086: ordinal not in range(128)
ERROR    2011-12-09 16:58:16,664 dev_appserver.py:4096] Exception encountered handling request
Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 4039, in _HandleRequest
    self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 3945, in _Dispatch
    base_env_dict=env_dict)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 605, in Dispatch
    base_env_dict=base_env_dict)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 3015, in Dispatch
    self._module_dict)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 2925, in ExecuteCGI
    reset_modules = exec_script(handler_path, cgi_path, hook)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver.py", line 2787, in ExecuteOrImportScript
    script_module.main()
  File "/Users/bryce/Documents/Aptana Studio 3 Workspace/DennysBE/djangoappengine/main/main.py", line 87, in real_main
    run_wsgi_app(application)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/util.py", line 98, in run_wsgi_app
    run_bare_wsgi_app(add_wsgi_middleware(application))
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/util.py", line 116, in run_bare_wsgi_app
    result = application(env, _start_response)
  File "/Users/bryce/Documents/Aptana Studio 3 Workspace/DennysBE/django/core/handlers/wsgi.py", line 273, in __call__
    response = self.get_response(request)
  File "/Users/bryce/Documents/Aptana Studio 3 Workspace/DennysBE/django/core/handlers/base.py", line 169, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/Users/bryce/Documents/Aptana Studio 3 Workspace/DennysBE/django/core/handlers/base.py", line 203, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/Users/bryce/Documents/Aptana Studio 3 Workspace/DennysBE/django/views/debug.py", line 59, in technical_500_response
    html = reporter.get_traceback_html()
  File "/Users/bryce/Documents/Aptana Studio 3 Workspace/DennysBE/django/views/debug.py", line 114, in get_traceback_html
    frames = self.get_traceback_frames()
  File "/Users/bryce/Documents/Aptana Studio 3 Workspace/DennysBE/django/views/debug.py", line 242, in get_traceback_frames
    pre_context_lineno, pre_context, context_line, post_context = self._get_lines_from_file(filename, lineno, 7, loader, module_name)
  File "/Users/bryce/Documents/Aptana Studio 3 Workspace/DennysBE/django/views/debug.py", line 223, in _get_lines_from_file
    context_line = source[lineno].strip('\n')
IndexError: list index out of range

这会导致上述错误:

request = urllib2.Request("http://google.com/foo/", headers={"Accept" : "text/html"})
contents = urllib2.urlopen(request).read()

这不会导致上述错误。唯一的区别是网址:

request = urllib2.Request("http://google.com/", headers={"Accept" : "text/html"})
contents = urllib2.urlopen(request).read()

所以问题似乎是域名之后的任何问题。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我能够通过使用urllib而不是urllib2让我的问题消失。我仍然不知道是什么原因引起了这个问题,但这次训练很好。

url = 'http://google.com/'
f = urllib.urlopen(url, params)