title: UnicodeDecodeError: 'ascii' codec can't decode byte XX with urllib2 urlopen()
body: <p>I'm trying to post some data to a url, but I'm running int a python error that doesn't make any sense to me.</p>
  
  <p>Here is the error:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>This causes the above error:</p>
  
  <pre><code>request = urllib2.Request("http://google.com/foo/", headers={"Accept" : "text/html"})
  contents = urllib2.urlopen(request).read()
  </code></pre>
  
  <p>This does NOT cause the above error. The only difference is the url:</p>
  
  <pre><code>request = urllib2.Request("http://google.com/", headers={"Accept" : "text/html"})
  contents = urllib2.urlopen(request).read()
  </code></pre>
  
  <p>So the problem appears to be anything anything after the domain name. Any ideas why?</p>