簡體   English   中英

openshift python echo websocket服務器

[英]openshift python echo websocket server

我正在嘗試在python中創建在OpenShift上正確運行的回顯服務器。 現在的樣子:

from geventwebsocket.handler import WebSocketHandler
from gevent.pywsgi import WSGIServer
from flask import Flask, request, render_template
import os

app = Flask(__name__)

@app.route('/a')
def index():
    return render_template('index.html')

@app.route('/')
def api():
    if request.environ.get('wsgi.websocket'):
        ws = request.environ['wsgi.websocket']
        print ws
        while True:
            message = ws.receive()
            ws.send(message)
    return

if __name__ == '__main__':
    ip   = os.environ['OPENSHIFT_PYTHON_IP']
    port = int(os.environ['OPENSHIFT_PYTHON_PORT'])
    print port, ip
    http_server = WSGIServer((ip,port), app, handler_class=WebSocketHandler)
    http_server.serve_forever()

但是,它似乎不起作用。 我沒有收到任何錯誤消息。(除了在客戶端。)我想知道是否有人可以幫助您。 我一直在尋找解決方案已有一個半星期的時間了,我真的很高興。 我也將感謝任何可行的示例。 注意 ,由於端口,域的限制,互聯網上的許多示例都無法在OpenShift上運行。 請幫我解決一下這個。 如果您對解決方案有把握, 回答。

您將需要連接到特定的端口,因為主路由層仍基於Apache,並且不支持WebSockets。

因此,對於普通WebSockets ws://,您將使用端口8000,而對於安全連接,wss://將使用端口8443。這是一個示例:

http://app-lovingwebsockets.rhcloud.com/  <= your current HTTP URL
http://app-lovingwebsockets.rhcloud.com:8000/ <= WebSockets enables HTTP URL

https://app-lovingwebsockets.rhcloud.com/  <= your current HTTPs URL
https://app-lovingwebsockets.rhcloud.com:8443/ <= WebSockets enables HTTPs URL

這是一個很棒的Python示例https://github.com/Lawouach/WebSocket-for-Python

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM