我试图用Python实现websocket安全服务器(基于这模块)
在链接中,它说模块支持TLS和SSL连接,但没有解释如何使用。
我目前的代码是这样的:
from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
import ssl
class SimpleResponder(WebSocket):
def handleMessage(self):
self.sendMessage(self.data)
def handleConnected(self):
print self.address, 'connected'
def handleClose(self):
print self.address, 'closed'
server = SimpleWebSocketServer('', 8000, SimpleResponder)
try:
server.serveforever()
except KeyboardInterrupt:
pass
print "Server ended"我试图用与HTTP服务器相同的方式实现SSL包装(就像这样):
server.socket = ssl.wrap_socket (server.socket, certfile='path/to/localhost.pem', server_side=True)但它似乎不起作用(我得到了下一个错误):
AttributeError: 'SimpleWebSocketServer' object has no attribute 'socket'因此很明显,SimpleWebSocketServer类的结构与SimpleHTTPServer类不同。
那么如何实现一个安全的websocket服务器呢?
发布于 2016-04-28 17:53:37
TLS/SSL示例 1)生成带密钥的证书 openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem 2)运行安全的TSL/SSL服务器(在本例中,cert.pem文件位于同一个目录中) python SimpleExampleServer.py --示例聊天--ssl --cert ./cert.pem 3)通过https服务websocket.html,向浏览器提供证书。HTTPS服务器将在本地目录中查找cert.pem。确保websocket.html也位于运行服务器的同一目录中。 数独蟒蛇SimpleHTTPSServer.py 4)打开web浏览器到:https://localhost:443/websocket.html 5)更改ws://localhost:8000/ to wss://localhost:8000并单击connect。 注意:如果连接有问题,请确保证书是针对异常https://localhost:8000或任何要连接的主机:端口对在浏览器中添加的。
我发现Autbahn_Python非常容易使用。它同时适用于扭曲的和异步网络引擎。
https://stackoverflow.com/questions/36921893
复制相似问题