Repository: ipython/ipython

Issue:
IPython 1.2.0: notebook fail with "500 Internal Server Error"
Hi there!

I had huge issues after upgrading from IPython 1.1.0 to 1.2.0.

 I start my notebook-server as follows:

```
$ ipython notebook --pylab --no-browser                                                     
$APPDIR/lib64/python2.6/site-packages/IPython/frontend.py:30: UserWarning: The top-level `frontend` package has been deprecated. All its subpackages have been moved to the top `IPython` level.
warn("The top-level `frontend` package has been deprecated. "
2014-02-12 09:49:04.887 [NotebookApp] Using existing profile dir: u'$MYDIR/.config/ipython/profile_default'
2014-02-12 09:49:04.895 [NotebookApp] Using MathJax from CDN: http://cdn.mathjax.org/mathjax/latest/MathJax.js
2014-02-12 09:49:04.905 [NotebookApp] Serving notebooks from local directory: $MYDIR/notebooks
2014-02-12 09:49:04.905 [NotebookApp] The IPython Notebook is running at: http://127.0.0.1:8888/
2014-02-12 09:49:04.905 [NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
```

(Note that i changed server-specifi paths to  $APPDIR and $MYDIR manually in the above output)

So far so good. However, after the 1.2.0 upgrade, once I browse to 127.0.0.1:8888, all I see in the browser window is "500: Internal Server Error", while the ipython-process outputs:

```
127.0.0.1:8888
2014-02-12 09:49:06.172 [tornado.application] ERROR | Uncaught exception GET / (127.0.0.1)
HTTPRequest(protocol='http', host='127.0.0.1:8888', method='GET', uri='/', version='HTTP/1.1', remote_ip='127.0.0.1', headers={'Accept-Language': 'en-US,en;q=0.8', 'Accept-Encoding': 'gzip,deflate,sdch', 'Host': '127.0.0.1:8888', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11', 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'Connection': 'keep-alive', 'Cache-Control': 'max-age=0'})
Traceback (most recent call last):
  File "$APPDIR/lib64/python2.6/site-packages/tornado-3.2-py2.6-linux-x86_64.egg/tornado/web.py", line 1218, in _when_complete
callback()
  File "$APPDIR/lib64/python2.6/site-packages/tornado-3.2-py2.6-linux-x86_64.egg/tornado/web.py", line 1239, in _execute_method
self._when_complete(method(*self.path_args, **self.path_kwargs),
  File "$APPDIR/lib64/python2.6/site-packages/tornado-3.2-py2.6-linux-x86_64.egg/tornado/web.py", line 2382, in wrapper
if not self.current_user:
  File "$APPDIR/lib64/python2.6/site-packages/tornado-3.2-py2.6-linux-x86_64.egg/tornado/web.py", line 983, in current_user
self._current_user = self.get_current_user()
  File "$APPDIR/lib64/python2.6/site-packages/IPython/html/base/handlers.py", line 112, in get_current_user
user_id = self.get_secure_cookie(self.cookie_name)
  File "$APPDIR/lib64/python2.6/site-packages/IPython/html/base/handlers.py", line 127, in cookie_name
self.request.host
ValueError: zero length field name in format
2014-02-12 09:49:06.174 [tornado.access] ERROR | 500 GET / (127.0.0.1) 2.42ms
2014-02-12 09:49:06.187 [tornado.access] WARNING | 404 GET /favicon.ico (127.0.0.1) 0.84ms
```

I tried deleting my ~/.config/ipython dir and upgrading dependency packages. But nothing helped get over this issue. Once I reverted to IPython 1.1.0 everything worked as expected.

(This server runs Python 2.6, tornado 3.2 and zmq 4.0.3)



Discussion:
> $APPDIR/lib64/python2.6/site-packages/IPython/frontend.py:30: UserWarning: The top-level `frontend` package has been deprecated.

This suggests there's some clash and leftovers from an older IPython are still around. IPython itself no longer references the frontend.py package. Can you make sure that the IPython directory destination is clean before installing into it, so there are no stale .py and .pyc files there.

 When you install 1.2, can you verify that the terminal version works (just `ipython`). If it does, then see if `ipython console` works - this will test the two-process kernel and frontend decoupling, without bringing the webserver complexities into the mix.  This will help us diagnose where the problem is, and please include the output of `import IPython; print IPython.sys_info();`

OK, looks like the patch you need is in #5104 - we just need to put it in the 1.x branch


Resolving patch:
diff --git a/IPython/html/base/handlers.py b/IPython/html/base/handlers.py
--- a/IPython/html/base/handlers.py
+++ b/IPython/html/base/handlers.py
@@ -122,7 +122,7 @@ def get_current_user(self):
 
     @property
     def cookie_name(self):
-        default_cookie_name = non_alphanum.sub('-', 'username-{}'.format(
+        default_cookie_name = non_alphanum.sub('-', 'username-{0}'.format(
             self.request.host
         ))
         return self.settings.get('cookie_name', default_cookie_name)


Test patch: