【问题标题】:Setting up SocketIO and Flask I'm getting this error: "UnboundLocalError: local variable 'ssl_socket' referenced before assignment"设置 SocketIO 和 Flask 我收到此错误:“UnboundLocalError: local variable 'ssl_socket' referenced before assignment”
【发布时间】:2019-07-30 15:27:05
【问题描述】:

我正在使用烧瓶设置 socketio,并且遇到了一些阻止它运行的错误。

File "src/gevent/greenlet.py", line 705, in gevent._greenlet.Greenlet.run     
  File "/Users/hairy/anaconda2/lib/python2.7/site-packages/gevent/baseserver.py", line 26, in _handle_and_close_when_done
  return handle(*args_tuple)
File "/Users/hairy/anaconda2/lib/python2.7/site-packages/gevent/server.py", line 193, in wrap_socket_and_handle
   ssl_socket.close()
UnboundLocalError: local variable 'ssl_socket' referenced before assignment  
2019-07-30T15:07:15Z <Greenlet "Greenlet-0" at 0x102908500: _handle_and_close_when_done(<bound method WSGIServer.wrap_socket_and_handle of, <bound method WSGIServer.do_close of <WSGIServer a, (<socket at 0x1067882d0 fileno=[Errno 9] Bad file )> failed with UnboundLocalError

这是我的烧瓶代码:

from flask import Flask, render_template, Markup
from util import getHtml, duplicate, displayArray
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

@app.route('/')
def home():
    return displayArray()
@socketio.on('connect')
def test_connect():
    emit('after connect',  {'data':'Lets dance'})
@socketio.on('plz reload', namespace='/test')
def test_message(message):
    emit('reload', {'data': message['data']})

if __name__ == '__main__':
    socketio.run(app, host='0.0.0.0', Debug=True)

这是我的js代码:

$(document).ready(function(){
    var socket = io.connect('http://localhost:5000');
//  var socket = io.connect('http://' + document.domain + ':' + location.port + '/test');
//      var socket = io.connect('http://localhost:5000');
    socket.on('connect', function() {
            socket.emit('my event', {data: 'I\'m connected!'});
        });
    socket.on('reload', function(msg) {
        $('#log').append('<p>Received: ' + msg.data + '</p>');
        location.reload();
    });
});

当我转到 localhost:5000 时应用程序未加载

【问题讨论】:

    标签: javascript python flask socket.io flask-socketio


    【解决方案1】:

    我通过修改修复了它

        var socket = io.connect('http://localhost:5000');
    

    var socket = io.connect();
    

    在 js 中。我不知道为什么这解决了它,但它确实。

    【讨论】:

    • 您是否使用 SSL 运行您的服务器?第一个选项是不加密连接,第二个选项通过主页的相同 URL 进行连接,因此如果是 https://,则到 Socket.IO 的连接也是 https://。
    • 不,我没有使用 ssl。
    猜你喜欢
    • 2017-08-08
    • 2021-05-11
    • 2013-02-11
    • 1970-01-01
    • 2019-07-11
    • 2023-02-13
    • 2018-01-22
    • 2021-12-17
    • 2015-06-26
    相关资源
    最近更新 更多