如何使 Python 套接字在 Flask 应用程序的后台接受连接?

发布于 2025-01-10 20:19:04 字数 829 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

审判长 2025-01-17 20:19:04

如果您要设置套接字服务器来接受 websocket 连接,请考虑使用 Flask-SocketIO

If you are setting up a socket server to accept websocket connections, consider using Flask-SocketIO.

梦亿 2025-01-17 20:19:04

socket.accept 调用是一个阻塞函数调用,这意味着该函数在客户端连接到它之前不会返回。因此,当此阻塞函数调用未返回时,您的render_template甚至不会启动。该函数不返回仅仅意味着它挂在那里。

Client, addresse = socket.accept()

如果您确实想使用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 your render_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.

Client, addresse = socket.accept()

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

爱本泡沫多脆弱 2025-01-17 20:19:04

您正在使用 while True 循环来阻止以下命令。

You are using a while Trueloop that blocks the following command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文