python websocket failure when run in openshift

孤人 提交于 2020-06-17 03:05:32

问题


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";
}

The openshift configuration is as follows:

1 pod running with app.py listening on port 8080 tls not enabled I have a non-tls route 8080 > 8080.

Firefox gives the following message in the console:

Firefox can’t establish a connection to the server at ws://openshiftprovidedurl.net:8080/ws.

when I use wscat to connect to the websocket.

wscat -c ws://openshiftprovidedurl.net/ws

I get the following error:

error: Error: unexpected server response (400)

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))

Any assistance would be appreciated!


回答1:


Graham Dumpleton hit the nail on the head, I modified the code from

factory = WebSocketServerFactory(u"ws://0.0.0.0:8080")

to

factory = WebSocketServerFactory(u"ws://0.0.0.0:8080", externalPort=80)

and it corrected the issue. I had to modify my index to point to the correct websocket but I am now able to connect.

Thanks!




回答2:


Based on the source code of autobahn-python, you can get that message only in 2 cases.

Here is the implementation:

if not ((self.factory.isSecure and self.factory.externalPort == 443) or (not self.factory.isSecure and self.factory.externalPort == 80)):
                    return self.failHandshake("missing port in HTTP Host header '%s' and server runs on non-standard port %d (wss = %s)" % (str(self.http_request_host), self.factory.externalPort, self.factory.isSecure))

Because I think you are using Deployment + Service (and maybe Ingress on top of them) for your server, you can bind your server to port 80 instead of 8080 and set that port in Service and in Ingress, if you are using them.



来源:https://stackoverflow.com/questions/49620870/python-websocket-failure-when-run-in-openshift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!