如何使 Python 套接字在 Flask 应用程序的后台接受连接?
我是 Flask 的初学者。 我正在 Flask 中开发一个小网站,我的目标是在后台设置一个接受连接的套接字。这是代码:
@app.route('/dashboard/')
@login_required
def dashboard_page():
s = lancer_socket()
accepter_msg(s) #if i comment out this line, the template loads
return render_template('dashboard.html',info = info,tel_conns=tel_conns)
但是,函数“accepter_msg”使模板无法加载。这是代码:
def accepter_msg(socket):
while True:
Client, addresse = socket.accept()
print('Connecté à: ' + addresse[0] + ':' + str(addresse[1]))
start_new_thread(threaded_client, (Client, ))
我的目标是加载仪表板页面,并使套接字接受连接并在后台接收数据(之后,我将使用修改后的变量(信息和tel_conns) :
render_template('dashboard.html',info = info,tel_conns=tel_conns)
我需要使用一个简单的 python 套接字,而不是 socketio 或其他任何东西。
I am a beginner in Flask.
I am working on a small website in Flask, my goal is to set up a socket accepting connections in background. Here is the code :
@app.route('/dashboard/')
@login_required
def dashboard_page():
s = lancer_socket()
accepter_msg(s) #if i comment out this line, the template loads
return render_template('dashboard.html',info = info,tel_conns=tel_conns)
However, the function 'accepter_msg' makes the template not load. Here is the code:
def accepter_msg(socket):
while True:
Client, addresse = socket.accept()
print('Connecté à: ' + addresse[0] + ':' + str(addresse[1]))
start_new_thread(threaded_client, (Client, ))
My goal is to load the dashboard page, and to make the socket accept the connections and receive data in background (after that, i will refresh the template with the modified variables (info and tel_conns) :
render_template('dashboard.html',info = info,tel_conns=tel_conns)
I need to use a simple python socket and not a socketio or anything else. Can anyone help me ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您要设置套接字服务器来接受 websocket 连接,请考虑使用 Flask-SocketIO 。
If you are setting up a socket server to accept websocket connections, consider using Flask-SocketIO.
socket.accept
调用是一个阻塞函数调用,这意味着该函数在客户端连接到它之前不会返回。因此,当此阻塞函数调用未返回时,您的render_template
甚至不会启动。该函数不返回仅仅意味着它挂在那里。如果您确实想使用socket.accept,则需要将其设置为非阻塞套接字。怎么做呢?这篇 realpython 文章有很好的例子。
https://realpython.com/python-sockets/
另请阅读此官方文档 Socket编程 HOWTO 以很好地理解套接字编程。
https://docs.python.org/3/howto/sockets.html
The
socket.accept
call is a blocking function call, which means the function does not return until a client connects to it. So yourrender_template
will not even start when this blocking function call does not return. The function does not return simply means that it is hanging there.If you really want to use
socket.accept
, you need it make it a non-blocking socket. How to do that? This realpython article has nice examples.https://realpython.com/python-sockets/
Also read this official documentation Socket Programming HOWTO to get a good understanding of socket programming.
https://docs.python.org/3/howto/sockets.html
您正在使用 while True 循环来阻止以下命令。
You are using a
while True
loop that blocks the following command.