I try to test my flask web application with websockets My code run well, but when I reload two or more times page in browser. I have in terminal OSError. And this error doesn't prevent to flask work on.
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
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)
What does error mean ?
Blind shot, but this behavior is most likely caused by the webbrowser terminating the websocket-connection before sending anything. Refreshing the browser probably causes the websocket to close it's (otherwise reusable) tcp connection. On the flask-side, this causes the OSError, which expects some bytes but dies while waiting for data as the socket is closed.
In other words: Nothing you can do about and actually not harmful.
I tried to solve error by various ways, but it didn't work for me. So after some time, I reinstalled Ubuntu and my server started to work.
Probably problem based on operation system sockets or I've not installed something properly. It keeps misunderstanding to me.
Leaving this comments to help somebody, but it has still opened and not fully solved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With