在openshift中运行时python websocket失败 [英] python websocket failure when run in openshift
问题描述
我有一个在python中运行的高速公路扭曲的websocket,该websocket在dev vm中正常运行,但是当服务器在openshift中运行时,我无法正常工作.
I have a autobahn twisted websocket running in python which is working in a dev vm correctly but I have been unable to get working when the server is running in openshift.
这是在虚拟机中对我有用的简化代码.
Here is the shortened code which works for me in a vm.
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory, listenWS
from autobahn.twisted.resource import WebSocketResource
class MyServerProtocol(WebSocketServerProtocol):
def onConnect(self, request):
stuff...
def onOpen(self):
stuff...
def onMessage(self,payload):
stuff...
factory = WebSocketServerFactory(u"ws://0.0.0.0:8080")
factory.protocol = MyServerProtocol
resource = WebSocketResource(factory)
root = File(".")
root.putChild(b"ws", resource)
site = Site(root)
reactor.listenTCP(8080, site)
reactor.run()
客户端的连接部分如下:
The connection part of the client is as follows:
var wsuri;
var hostname = window.document.location.hostname;
wsuri = "ws://" + hostname + ":8080/ws";
if ("WebSocket" in window) {
sock = new WebSocket(wsuri);
} else if ("MozWebSocket" in window) {
sock = new MozWebSocket(wsuri);
} else {
log("Browser does not support WebSocket!");
window.location = "http://autobahn.ws/unsupportedbrowser";
}
openshift配置如下:
The openshift configuration is as follows:
1个运行app.py的Pod在端口8080上侦听 未启用tls 我有一条非tls航线8080> 8080.
1 pod running with app.py listening on port 8080 tls not enabled I have a non-tls route 8080 > 8080.
Firefox在控制台中显示以下消息:
Firefox gives the following message in the console:
Firefox can’t establish a connection to the server at ws://openshiftprovidedurl.net:8080/ws.
当我使用wscat连接到Websocket时.
when I use wscat to connect to the websocket.
wscat -c ws://openshiftprovidedurl.net/ws
我收到以下错误:
error: Error: unexpected server response (400)
和openshift中的应用程序日志显示以下内容:
and the application log in openshift shows the following:
2018-04-03 01:14:24+0000 [-] failing WebSocket opening handshake ('missing port in HTTP Host header 'openshiftprovidedurl.net' and server runs on non-standard port 8080 (wss = False)')
2018-04-03 01:14:24+0000 [-] dropping connection to peer tcp4:173.21.2.1:38940 with abort=False: missing port in HTTP Host header 'openshiftprovidedurl.net' and server runs on non-standard port 8080 (wss = False)
2018-04-03 01:14:24+0000 [-] WebSocket connection closed: connection was closed uncleanly (missing port in HTTP Host header 'openshiftprovidedurl.net' and server runs on non-standard port 8080 (wss = False))
任何帮助将不胜感激!
推荐答案
Graham Dumpleton碰到了头,我修改了
Graham Dumpleton hit the nail on the head, I modified the code from
factory = WebSocketServerFactory(u"ws://0.0.0.0:8080")
到
factory = WebSocketServerFactory(u"ws://0.0.0.0:8080", externalPort=80)
,它更正了该问题.我必须修改索引以指向正确的websocket,但现在可以连接了.
and it corrected the issue. I had to modify my index to point to the correct websocket but I am now able to connect.
谢谢!
这篇关于在openshift中运行时python websocket失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!