怎么抓插座.iopython中的异常

2024-06-13 10:25:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个班级项目,我们正在努力赶上插座.io例外情况。明确地,socketio.exceptions.ConnectionError:服务器拒绝连接 我们这样做的目的是在没有连接的情况下继续尝试连接。在

我们试图查看位于https://python-socketio.readthedocs.io/en/latest/的文档,但找不到任何内容。同样,如果还没有建立连接,我们将尝试连接到服务器。我们在看插座。重新连接()但这似乎只在建立连接后才起作用。 我们正试图在树莓皮3上实现这一点。我们编写了一个脚本来在启动时执行我们的客户端代码,但是在脚本执行之后,以太网连接到了pi。在


import socketio
import opc, time
import json

# Initiate socket client
socket = socketio.Client()

# Initiate socket client for fadecandy server (little chip between Led strip and Pi
fade_client = opc.Client('localhost:xxxx')

# Establish connection to server
socket.connect("website url", namespaces=['/pi'])

# stuff to do after connection, mainly change led lights through
# fadecandy server

Tags: ioimport服务器脚本clientserverpisocket
1条回答
网友
1楼 · 发布于 2024-06-13 10:25:11

这应该对你有用:

connected = False
while not connected:
    try:
        socket.connect("website url", namespaces=['/pi'])
    except socketio.exceptions.ConnectionError as err:
        print("ConnectionError: %s", err)
    else:
        print("Connected!")
        connected = True

Google“python try except”了解如何捕捉异常。在

相关问题 更多 >