Error tagging WebSocketHandler.open with @authenticated

97 views
Skip to first unread message

Russ Weeks

unread,
Jun 25, 2012, 3:02:23 PM6/25/12
to python-...@googlegroups.com
Hi,

I'm trying to use @tornado.web.authenticated on my subclass implementation of WebSocketHandler.open.  The problem is that @authenticated will try to invoke the wrapped method with *args and **kwargs, but WebSocketHandler.open expects those values to be in self.open_args and self.open_kwargs.  I get this error when my client tries to open the socket:

ERROR:root:Uncaught exception in /stream/author
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/tornado/websocket.py", line 258, in wrapper
    return callback(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tornado/web.py", line 1794, in wrapper
    return method(self, *args, **kwargs)
TypeError: open() takes exactly 1 argument (2 given)

Is this a known problem?  Any advice re. a workaround?

Thanks,
-Russ

Phil Whelan

unread,
Jun 25, 2012, 10:11:55 PM6/25/12
to python-...@googlegroups.com, python-...@googlegroups.com
On 2012-06-25, at 12:02 PM, Russ Weeks <rwe...@newbrightidea.com> wrote:

> return method(self, *args, **kwargs)
> TypeError: open() takes exactly 1 argument

My guess... Have you tried removing "self, " from the above?

Phil

Russ Weeks

unread,
Jun 26, 2012, 12:09:57 PM6/26/12
to python-...@googlegroups.com
Hi, Phil,

That line of code is in tornado's web.py, so I don't want to change it unless I have to... my current workaround is to just redefine my WebSocketHandler's open method to take *args and **kwargs, even though those variables aren't used.  Works well enough!

Thanks,
-Russ 

Peter Bengtsson

unread,
Jun 27, 2012, 12:00:49 PM6/27/12
to python-...@googlegroups.com
It just doesn't feel right. Why don't you just write your own decorator but for your WebSocket handlers? 
It clearly wasn't built for WebSockets. 

Ben Darnell

unread,
Jun 27, 2012, 12:54:58 PM6/27/12
to python-...@googlegroups.com
Your code should not be touching open_args and open_kwargs - those
variables are implementation details of the WebSocketHandler. If your
urlspec extracts parameters to be passed in, you should define your
open() override to take those arguments, just like you would with
get() or post() for regular handlers. This is true regardless of
whether you're using @authenticated or not.

Also, @authenticated probably doesn't make sense for websockets, since
you can't redirect the user to a login page over a websocket. You'll
need to put @authenticated on the HTML page that contains the
websocket instead (and then if a websocket connection is attempted
without a valid user just return an error).

-Ben
Reply all
Reply to author
Forward
0 new messages