EzDevInfo.com

sockjs-tornado

WebSocket emulation - Python server

SockJS-Tornado + Flask Error: No 'Access-Control-Allow-Origin' header is present on the requested resource

I have an existing Tornado + Flask app that I wanted to upgrade and use SockJS-Tornado.

I have followed the guide to setup SockJS-Tornado but when I try to access the new upgraded tornado server from the Flask App I get the following error

No 'Access-Control-Allow-Origin' header is present on the requested resource.

I think it is a cors issue (since the flask served page is now requesting a resource from SockJS-Tornado which is on a different port) but I cannot find a way to set this up with SockJS-Tornado.

Is there any way to setup cors for SockJS-Tornado? The documentation seems to suggest that this should work automatically, without any need for reconfiguration.

There is a bug reported https://github.com/mrjoes/sockjs-tornado/issues/61 which probably means that there is indeed an issue here. But since there are no comments or workarounds mentioned there, I have come here for help :-)


Source: (StackOverflow)

OpenSSL error on Tornado server

I configure my Tornado server with:

ssl_options = { 
    "certfile": os.path.join("/tls.crt"),
    "keyfile": os.path.join("/tls.key")
}   
http = tornado.httpserver.HTTPServer(application, ssl_options=ssl_options)

tls.crt and tls.key are wildcarded for my domain, which I use successfully in another app in my stack behind HAPROXY, the latter terminating TLS.

The server on startup reports OpenSSL version:

OpenSSL 1.0.1k 8 Jan 2015

Browser

However, when fetch / from the browser (which eventually says "This web page is not available"), this appears in the Tornado STDOUT:

 [E 150228 15:05:52 ioloop:588] Exception in callback (<socket._socketobject object at 0x7ff342d37050>, <function null_wrapper at 0x7ff342d418c0>)
     Traceback (most recent call last):
       File "/usr/local/lib/python2.7/site-packages/tornado/ioloop.py", line 840, in start
         handler_func(fd_obj, events)
       File "/usr/local/lib/python2.7/site-packages/tornado/stack_context.py", line 275, in null_wrapper
         return fn(*args, **kwargs)
       File "/usr/local/lib/python2.7/site-packages/tornado/netutil.py", line 223, in accept_handler
         callback(connection, address)
       File "/usr/local/lib/python2.7/site-packages/tornado/tcpserver.py", line 225, in _handle_connection
         do_handshake_on_connect=False)
       File "/usr/local/lib/python2.7/site-packages/tornado/netutil.py", line 459, in ssl_wrap_socket
         context = ssl_options_to_context(ssl_options)
       File "/usr/local/lib/python2.7/site-packages/tornado/netutil.py", line 436, in ssl_options_to_context
         context.load_cert_chain(ssl_options['certfile'], ssl_options.get('keyfile', None))
     SSLError: [SSL] PEM lib (_ssl.c:2506)

cURL

Curling the endpoint shows:

* About to connect() to example.org port 443 (#0)
*   Trying 54.154.175.173... connected
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):
* Unknown SSL protocol error in connection to example.org:443 
* Closing connection #0
curl: (35) Unknown SSL protocol error in connection to example.org:443

openssl s_client

openssl s_client -connect example.org:443

Just hangs on:

CONNECTED(00000003)

I've cloned https://github.com/openssl/openssl.git and checked out the 1.0.1k tag, but can't find _ssl.c, so pointers here will be a good start.

I've also pointed CryptoNark at my domain, but just get empty output.

The host OS is AWS AMI ami-6330b7141. The Docker container for the app is python:2.7 version 31ff30c97af1.

UPDATE

The line in _ssl.c seems to be part of Python 3 backported stuff by @benjamin-peterson I'll try with latest Python 3.4.


Source: (StackOverflow)

Advertisements

Create own websocket for octoprint plugin

I'm currently developing a plugin for octoprint, which uses sockjs-tornado for the server side to create a websocket. Now I try to create my own one (because the default implementation of the octoprint socket does not support forwarding events to my plugin on incoming messages). Everything I get is a 404 and unfortunately google is not a very good friend at this.

I can create a flask blueprint plugin and as far as I understand it, every http request will be directly redirected to my plugin, but I don't know how to connect the flask blueprint and tornado socket.

Does anyone has a good piece of code to connect the tornado and flask blueprint?


Source: (StackOverflow)

Swampdragon - Publish to specific channel/Group

I am looking into the swampdragon chat_example. In the router.py as per documentation get_subscription_channel gives channel name. when i tried to change the retrun value it still works.

how can i limit the messages to specific group/channel. What things i need to do in the front end.

from swampdragon import route_handler
from swampdragon.route_handler import BaseRouter


class ChatRouter(BaseRouter):
    route_name = 'chat-route'
    valid_verbs = ['chat', 'subscribe']

    def get_subscription_channels(self, **kwargs):
        return ['chatrm']

    def chat(self, *args, **kwargs):

        errors = {}

        if errors:
            self.send_error(errors)
        else:
            self.send({'status': 'ok'})
            self.publish(self.get_subscription_channels(), kwargs)


route_handler.register(ChatRouter)

here is the subscription method.

function subscribe () {
    swampdragon.subscribe('chat-route', 'local-channel', null, function (context, data) {
        // any thing that happens after successfully subscribing
    }, function (context, data) {
        // any thing that happens if subscribing failed
    });
}

Thanks in advance.


Source: (StackOverflow)