Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9054117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:36:07+00:00 2026-06-16T13:36:07+00:00

I am writing a small app where I use Tornado websockets. I use cookies

  • 0

I am writing a small app where I use Tornado websockets. I use cookies for authentication, and I therefore want to check this when establishing the web socket.

So here is the challenge (building on the official websockets example):

class EchoWebSocket(tornado.websocket.WebSocketHandler):
    def open(self):
        userx = self.get_secure_cookie("user")
        if not userx:
            self.send_error(403) #I want to redirect or otherwise stop the user
        print "WebSocket opened"

    def on_message(self, message):
        userx = self.get_secure_cookie("user")
        self.write_message(u"%s said: %s" % (userx, message))

    def on_close(self):
        print "WebSocket closed"

Turns out that this is not very popular with Tornado, throwing this error:

ERROR:root:Exception in callback <tornado.stack_context._StackContextWrapper object at    0x10fa10db8>
Traceback (most recent call last):
 ....
  File "/Library/Python/2.7/site-packages/tornado-2.4-py2.7.egg/tornado/iostream.py", line 565, in _check_closed
raise IOError("Stream is closed")
IOError: Stream is closed

Is it even possible to “authenticate or cancel” during websockets handshake? Is this a problem with my code, or is there a issue/missing feature with Tornado? Is Tornado capable of sending some error condition over websockets to trigger the onerror handler at the client side in this situation (can not find any way to force an error condition in the manual)?

The sequence of events I am trying to achieve is:

  1. the user logs in using regular web requests, and a session token is stored as a secure cookie

  2. the user loads a page that uses websockets, javascript attempts to establish the socket

  3. before the socket is established I want to check the presence of a valid session token

    • if valid token, the socket is established and my app works as intended
    • if invalid token, there is no socket, and the user gets an error (alternatively, an websocket error condition is raised by the server)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:36:09+00:00Added an answer on June 16, 2026 at 1:36 pm

    You can track your users, you can do it easily, as each user connects to the server is created an instance, check this SO link.

    Regarding that you want to close the connection if he is not authenticated, you a can do it by calling close() method inside WebSWebSocketHandler instance like this:

    class WSHandler(tornado.websocket.WebSocketHandler):
    
    def open(self):
        if not condition:
            self.close()
    

    Check this in Tornado: tornado.websocket.WebSocketHandler.WebSocketHandler.close()

    I didn’t try it by myself. You can try it and tell me if it works for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a small web app for myself in Rails and I want it
I am writing a small .NET Windows Forms app, and I use the built-in
I'm writing a small sample app using Ember.js. My goal is to determine use
I'm writing a small web app with Node.js using the Express framework. I'm using
I'm writing a pretty small and overall simple app, and I want to watch
I'm writing a small web app using CytoscapeWeb. It downloads an XML file containing
I'm writing a small web app that will receive and parse tab-delimited text files
I'm in the process of writing a small web app that would allow me
I'm writing a small non-commercial web app, and I'm wondering how to pass data
I'm writing a small application in CakePHP 2.1, and I want to use Nick

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.