gpt4 book ai didi

python - 操作系统错误 : raw readinto() returned invalid length when use websockets

转载 作者:太空狗 更新时间:2023-10-29 19:30:00 24 4
gpt4 key购买 nike

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

ma​​in.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 关闭它的(否则可重用)tcp 连接。在 flask 方面,这会导致 OSError,它需要一些字节但在套接字关闭时等待数据时死掉。

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com