title: can't connect to Tornado Websocket from web Application : Error 403
body: <p>I have a web page should update its contents from a Python server using Tornado WebSocket.</p>
  <ol>
  <li>I have built the web pages using PHP/MVC</li>
  <li>It works perfectly with Local Apache (access the page with localhost)</li>
  <li>Tornado Server runs successfully and process all income requests</li>
  <li>I have added a pure JavaScript at the end of my page to open a WebSocket</li>
  </ol>
  <p><strong>My Problem</strong></p>
  <blockquote>
  <p>Tornado Server reply with <code>304</code> and print (e.g:'Hello') <strong>if and only if</strong> the request comes from a direct access (I mean to send request manually, out of web page like: <code>192.168.8.103:8888</code>)</p>
  <p>When I try to open a WebSocket from the web page, Torando Server reply with <code>200</code> and fail to print the message (e.g:'Hello') as the attached images.</p>
  </blockquote>
  <p><a href="https://i.stack.imgur.com/YtHiE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YtHiE.png" alt="Tornado error message (server-side)" /></a></p>
  <p><a href="https://i.stack.imgur.com/zqnWh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zqnWh.png" alt="Web page error message" /></a></p>
  <p><strong>Tornado Script (server):</strong></p>
  <pre class="lang-python prettyprint-override"><code>import tornado.httpserver
  import tornado.ioloop
  import tornado.options
  import tornado.web
  import tornado.websocket
  from tornado.options import define, options
  define(&quot;port&quot;, default=8888, help=&quot;run on the given port&quot;, type=int)
  class IndexHandler(tornado.web.RequestHandler):
      def get(self):
          self.write('Hello')
          #self.flush()
  class WebSocketHandler(tornado.websocket.WebSocketHandler):
      def open(self):
          print 'new connection'
          self.write_message(&quot;connected&quot;)
      def on_message(self, message):
          print 'message received %s' % message
          self.write_message('message received %s' % message)
      def on_close(self):
          print 'connection closed'
  if __name__ == &quot;__main__&quot;:
      tornado.options.parse_command_line()
      app = tornado.web.Application(
          handlers=[
              (r&quot;/&quot;, IndexHandler),
              (r&quot;/ws&quot;, WebSocketHandler)
          ]
      )
      httpServer = tornado.httpserver.HTTPServer(app)
      httpServer.listen(options.port)
      print &quot;Listening on port:&quot;, options.port
      tornado.ioloop.IOLoop.instance().start()
  </code></pre>
  <p><strong>WebSocket script (client):</strong></p>
  <pre class="lang-js prettyprint-override"><code>var socket = new WebSocket(&quot;ws://192.168.8.103:8888/ws&quot;);
    socket.onopen = function(){  
      console.log(&quot;connected&quot;); 
    }; 
    socket.onmessage = function (message) {
      console.log(&quot;receiving: &quot; + message.data);
    };
    socket.onclose = function(){
      console.log(&quot;disconnected&quot;); 
    };
    sendMessage = function(message) {
      socket.send(message);
    };
    try{
        var value = 0;
        window.onload = function() {
        setInterval(function() {
          console.log('Sending Value: ' + value);
        sendMessage(&quot;{sensor: Ultra, value:&quot; + value + &quot;}&quot;);
        value += 10;
        },10000)
    };
    } catch(err)
      {
      alert(&quot;Error with creating the WebSocket&quot;+err)
      }
  </code></pre>
  <p>I've searching for a solution for three days with no result :( ... I'm grateful for any help, Many Thanks for everyone</p>