import os
import socket
def socketcommunication():
tcp_client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_client_socket.connect(("192.168.199.111", 4001))
return tcp_client_socket
def Offlineprocessing(tcp_client_socket):
test=""
#判断网络是否正常
while True:
cmd = "ping www.baidu.com"
exit_code = os.system(cmd)
if exit_code: #断网
#判断tcp通信是否成功
while True:
try:
string = "srs_live"
b = bytes(string, encoding='utf - 8')
print("发送信息")
test.send(b)
print("发送成功")
break
except socket.error:
while True:
print("判断中")
cmd = "ping www.baidu.com"
exit_code = os.system(cmd)
if not exit_code: # 断网
test = socketcommunication()
print("有网了")
break
else:
print("没网")
except:
while True:
print("判断中1")
cmd = "ping www.baidu.com"
exit_code = os.system(cmd)
if not exit_code: # 断网
test=socketcommunication()
print("有网了1")
break
else:
print("没网1")
else:
print("网络正常")
if __name__ == "__main__":
tcp_client_socket = socketcommunication()
Offlineprocessing(tcp_client_socket)
python Tcp实现断网自动重连
猜你喜欢
转载自blog.csdn.net/weixin_44094965/article/details/103375496
今日推荐
周排行