在 tornadoweb websockets 服务器中实现 SSL

Posted

技术标签:

【中文标题】在 tornadoweb websockets 服务器中实现 SSL【英文标题】:Implement SSL in tornadoweb websockets server 【发布时间】:2013-12-03 06:18:16 【问题描述】:

祝大家今天好,

我有一个用 python 编写的 websocket 服务器,它为客户端对 postgresql 数据库表所做的更改提供服务。目前它使用标准的 ws 协议。我想实现 SSL wss,但 tornadoweb 站点上似乎没有文档。任何帮助表示赞赏!

代码如下:

import tornado.web
import tornado.websocket
import tornado.ioloop
import tornado.httpserver
import threading


import select
import psycopg2
import psycopg2.extensions

# This is a global variable to store all connected clients
websockets = []

# Connect to DB to listen for notifications
conn = psycopg2.connect("host=xxx.xxx.xxx.xxx dbname=mydb user=xxx port=yyy")
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)

curs = conn.cursor()
curs.execute("LISTEN notifier;")

# Create the websocket 
class WebSocketHandler(tornado.websocket.WebSocketHandler):
    @tornado.web.asynchronous
    # Add to global variable the connected client
    def open(self):
    self.set_nodelay(True)

    # on disconnect remove client
        def on_close(self):
        # Search for it in the list. If it matches with us (the client) then remove it as he has quit the con
        try:
        for websocket in websockets:
            if websocket[0] == self:
                websockets.remove(websocket)
    except ValueError:
        print ValueError

def on_message(self, message):
    if self not in websockets:
        websockets.append([self,message])

def on_pong(self, data):
    print data

# This is the function that polls the database for changes, then it sends change to clients through websockets
def db_listener():  
while 1:
    if select.select([conn],[],[],5) == ([],[],[]):
        for websocket in websockets:
            # ping the client regularly to avoid disconnect
            websocket[0].ping("ping")
    else:
        conn.poll()
        while conn.notifies:
            notify = conn.notifies.pop()
            details = notify.payload.split(",")
            if len(details) > 1:
                for websocket in websockets:
                    if details[34] in websocket[1]:
                            websocket[0].write_message(notify.payload)

application = tornado.web.Application([
    (r"/websocket", WebSocketHandler),
])

if __name__ == "__main__":
# Start a separate thread for every client so that we do not block the main websockets program!
    threading.Thread(target=db_listener).start()
    application.listen(5252)
    tornado.ioloop.IOLoop.instance().start()

有什么建议吗?

谢谢大家!!

【问题讨论】:

【参考方案1】:

试试这个:

server = tornado.httpserver.HTTPServer(application, ssl_options = 
        "certfile": "path-to-crt-file",
        "keyfile": "path-to-key-file",
    )
server.listen(5252)

【讨论】:

以上是关于在 tornadoweb websockets 服务器中实现 SSL的主要内容,如果未能解决你的问题,请参考以下文章

[WebSocket] 开发在线客服系统知识点-websocket返回状态码的含义

基于websocket实现一个简单的网站在线客服聊天室案例

webSocket客服在线交谈

(二十)ATP应用测试平台——websocket实现微服务版在线客服聊天室实战案例

全新EasyRTC平台功能开发:如何基于websocket实现视频客服通信

[Go] Golang练习项目-web客服系统即时通讯websocket项目go-fly