我尝试使用websockets测试我的Flask Web应用程序
我的代码运行良好,但是当我在浏览器中重新加载两次或多次页面时。我在终端OSError中。而且此错误不会阻止 flask 继续工作。

main.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Chat</title>
    <script type="text/javascript" src="{{ url_for('static', filename='jquery-2.2.0.js') }}"></script>
    <script type="text/javascript" src="{{ url_for('static', filename='socket.io.min.js') }}"></script>
</head>
<body>

<script type="text/javascript" charset="utf-8">

     $(document).ready(function() {

         var socket = io.connect('http://' + document.domain + ':' + location.port);

         socket.emit('connect', {data: 'U connected'});

         socket.on('apply', function (e) {
            console.log('it works');
                $('#log').append('<br>' + e.data + '<br>')

        });

     });

</script>

<h1 id="log"></h1>
</body>
</html>

app.py
from flask_socketio import SocketIO, emit
from flask import Flask, render_template


app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socket_io = SocketIO(app)


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


@socket_io.on('connect')
def connect():
    emit('apply', {'data': "Connect"})

if __name__ == '__main__':
    socket_io.run(app, debug=True)

追溯
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/eventlet/greenpool.py", line 82, in _spawn_n_impl
    func(*args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/eventlet/wsgi.py", line 703, in process_request
    proto.__init__(sock, address, self)
  File "/usr/local/lib/python3.5/socketserver.py", line 684, in __init__
    self.handle()
  File "/usr/local/lib/python3.5/http/server.py", line 417, in handle
    self.handle_one_request()
  File "/usr/local/lib/python3.5/site-packages/eventlet/wsgi.py", line 315, in handle_one_request
    self.raw_requestline = self.rfile.readline(self.server.url_length_limit)
OSError: raw readinto() returned invalid length -1 (should have been between 0 and 8192)

错误是什么意思?

最佳答案

盲目射击,但是这种行为很可能是由网络浏览器在发送任何内容之前终止了websocket-connection引起的。刷新浏览器可能会导致websocket关闭其(否则可重用)tcp连接。在 flask 端,这会导致OSError,该错误可能需要一些字节,但是在套接字关闭时等待数据时会死掉。

换句话说:您无能为力并且实际上无害。

关于python - OSError : raw readinto() returned invalid length when use websockets,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35394616/

10-12 17:54