我有一些代碼從Raspberry Pi接收WiFi密碼。 Pi每2分鐘發一個新代碼。下面的腳本檢查密碼並根據需要更新與新密碼的連接。當套接字斷開連接時退出Python While Loop
# Create a TCP/IP socket
s=socket(AF_INET, SOCK_DGRAM)
# Bind the socket to the port
s.bind (('',4446))
s.settimeout(10.0)
print ("Listening . . . .")
data=s.recv(1024).decode()
print ("Password: "+data)
os.system('netsh wlan set profileparameter name=PI_AP Keymaterial='+data)
var1=data
try:
while 1:
data=s.recv(1024).decode()
print ("Password: "+data)
if var1!=data:
os.system('netsh wlan set profileparameter name=PI_AP Keymaterial='+data)
print ("Password: "+data)
var1=data
except socket.timeout:
print ("Timed Out")
這裏是輸出,與我看到錯誤消息後我斷開:
>>> ================================ RESTART ================================
>>>
Listening . . . .
Password: m9FyvpJCILQrZB4sq125AfUn9nfS9Z6qDlbBxy12pL48y5kJTLrH01osp4xXWN3
Password: m9FyvpJCILQrZB4sq125AfUn9nfS9Z6qDlbBxy12pL48y5kJTLrH01osp4xXWN3
Password: m9FyvpJCILQrZB4sq125AfUn9nfS9Z6qDlbBxy12pL48y5kJTLrH01osp4xXWN3
**Traceback (most recent call last): File "C:\Users\cave\Desktop\system_V1\UAD-V1.0.py", line 21, in <module> data=s.recv(1024).decode() socket.timeout: timed out During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\cave\Desktop\system_V1\UAD-V1.0.py", line 29, in <module> except socket.timeout: TypeError: catching classes that do not inherit from BaseException is not allowed >>>**
難道你不能'而數據:'而不是? –