如何在python3中刷新套接字?
我想做这样的事情
def clear_buffer(sock):
try:
while sock.recv(1024): pass
except:
pass
或
def clear_buffer(sock):
while True:
data = sock.recv(1024)
if not data:
break
尝试运行这两个函数后,似乎都没有中断while循环和/或从传入数据中清除套接字。套接字停止接收数据后,我该如何有条件地执行某些操作?
答案 0 :(得分:0)
while run_network:
recv = True
data = b''
try:
while recv:
try:
packet = socket.recv(10240)
if packet:
data += packet
else:
recv = False
try:
pickle.loads(data)
recv = False
except:
recv = True
except ConnectionResetError:
print(f"{client} has disconnected........")
break
except ConnectionError:
**strong text**
print(f"{client}has a Connection Error.....")
break