Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果未建立web套接字连接,如何向http错误页发送消息或中止?_Python_Websocket_Bottle_Gevent - Fatal编程技术网

Python 如果未建立web套接字连接,如何向http错误页发送消息或中止?

Python 如果未建立web套接字连接,如何向http错误页发送消息或中止?,python,websocket,bottle,gevent,Python,Websocket,Bottle,Gevent,下面的示例取自他们的文档,并稍作修改。为什么web套接字连接未建立时它不中止 #!/usr/bin/python import json from bottle import route, run, request, abort, Bottle ,static_file from pymongo import Connection from gevent import monkey; monkey.patch_all() from time import sleep app = Bottle

下面的示例取自他们的文档,并稍作修改。为什么web套接字连接未建立时它不中止

#!/usr/bin/python

import json
from bottle import route, run, request, abort, Bottle ,static_file
from pymongo import Connection
from gevent import monkey; monkey.patch_all()
from time import sleep

app = Bottle()

@app.route('/websocket')
def handle_websocket():
    wsock = request.environ.get('wsgi.websocket')
    if not wsock:
        abort(400, 'Expected WebSocket request.')
    while True:
        try:
            message = wsock.receive()
            wsock.send("Your message was: %r" % message)
            sleep(3)
            wsock.send("Your message was: %r" % message)
        except WebSocketError:
            break

@app.route('/<filename:path>')
def send_html(filename):
    return static_file(filename, root='./', mimetype='text/html')


from gevent.pywsgi import WSGIServer
from geventwebsocket import WebSocketHandler, WebSocketError

host = "127.0.0.1"
port = 8080

server = WSGIServer((host, port), app,
                    handler_class=WebSocketHandler)
print("access @ http://%s:%s/websocket.html" % (host,port)
server.serve_forever()
#/usr/bin/python
导入json
从瓶子导入路径、运行、请求、中止、瓶子、静态文件
从pymongo导入连接
从gevent进口猴子;猴子
从时间上导入睡眠
app=瓶子()
@应用程序路径(“/websocket”)
def handle_websocket():
wsock=request.environ.get('wsgi.websocket')
如果不是wsock:
中止(400,“预期的WebSocket请求”。)
尽管如此:
尝试:
message=wsock.receive()
wsock.send(“您的邮件是:%r”%message)
睡眠(3)
wsock.send(“您的邮件是:%r”%message)
除WebSocketError外:
打破
@应用程序路径(“/”)
def send_html(文件名):
返回静态_文件(文件名,根='./',mimetype='text/html')
从gevent.pywsgi导入WSGIServer
从geventwebsocket导入WebSocketHandler、WebSocketError
host=“127.0.0.1”
端口=8080
服务器=WSGIServer((主机、端口)、应用程序、,
处理程序(类=WebSocketHandler)
打印(“access@http://%s:%s/websocket.html”%(主机,端口)
服务器。永远为您服务()

var ws=newwebsocket(“ws://localhost:8080/WebSocket”);
ws.onopen=函数(){
发送(“你好,世界”);
};
ws.onmessage=函数(evt){
警报(evt.数据);
};

连接未建立时是否可以向前端发送消息?

websocket设计为不容易中止。后端代码需要断开连接,或者websocket只是等待套接字打开,并在消息通过时继续正常运行

#!/usr/bin/python

import json
from bottle import route, run, request, abort, Bottle ,static_file
from pymongo import Connection
from gevent import monkey; monkey.patch_all()
from time import sleep

app = Bottle()

@app.route('/websocket')
def handle_websocket():
    wsock = request.environ.get('wsgi.websocket')
    if not wsock:
        abort(400, 'Expected WebSocket request.')
    while True:
        try:
            message = wsock.receive()
            wsock.send("Your message was: %r" % message)
            sleep(3)
            wsock.send("Your message was: %r" % message)
        except WebSocketError:
            break

@app.route('/<filename:path>')
def send_html(filename):
    return static_file(filename, root='./', mimetype='text/html')


from gevent.pywsgi import WSGIServer
from geventwebsocket import WebSocketHandler, WebSocketError

host = "127.0.0.1"
port = 8080

server = WSGIServer((host, port), app,
                    handler_class=WebSocketHandler)
print("access @ http://%s:%s/websocket.html" % (host,port)
server.serve_forever()
然而,代码中没有任何东西表明有什么问题,或者当连接建立或断开时,它只是看起来会在连接建立后立即发送“Hello,World”,然后以3秒的间隔两次接收回它