I have Sever/Client sending data back and forth, everything works fine, until the client looses connection and tries to reconnect. When I run the project in the coding windows I can disconnect any client or server and it will reconnect, but when I run it in crontab the server clients reconnect when server is rebooted. but when client is rebooted it will not reconnect. How can I get a error report why that is happening? or why will it work when running from Thonny or Greany and not from crontab? I have a 30 sec. sleep at crontab reboot command.
Server Code:
#!/usr/bin/env python3
import threading
import pygame.mixer
import automationhat
import datetime
from datetime import date
import time
import socket
...
time.sleep(10)
class ButtonServer(threading.Thread):
def __init__(self, channel, serverIP, serverPort):
threading.Thread.__init__(self)
self.channel = channel
self.ServIP = serverIP
self.ServPort = serverPort
self.conn = ""
self.Addr = ""
self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.serv.bind((serverIP,serverPort))
self.serv.listen(5)
self.ClientHasData = False
self.ClientConnected = False
self.BellButtonOne = -1
self.BellButtonTwo = -1
self.BellButtonThree = -1
self.deamon = True
self.start()
def run(self):
while True:
self.conn, self.addr = self.serv.accept()
from_client = ''
self.ClientHasData = True
while self.ClientHasData:
from_client = ''
data = self.conn.recv(4096)
if not data: self.ClientHasData = False
from_client = data.decode()
if from_client == "Input 1 On":
self.BellButtonOne = 1
if from_client == "Input 1 Off":
self.BellButtonOne = 0
if from_client == "Input 2 On":
self.BellButtonTwo = 1
if from_client == "Input 2 Off":
self.BellButtonTwo = 0
if from_client == "Input 3 On":
self.BellButtonThree = 1
if from_client == "Input 3 Off":
self.BellButtonThree = 0
if from_client == "Client Connected":
sc1.play(snd1)
self.conn.close()
B8 = ButtonServer(36,"123.456.789.123",12345)
sc1.play(snd1)
playDoing = False
DinnerBellNo = 0
while True:
if not B8: B8.start
...
Client Code:
#!/usr/bin/env python3
import automationhat
import time
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "123.456.789.123"
port = 12345
while True:
time.sleep(1)
try:
client.connect((host,port))
break
except:
print()
print("Connected")
client.send("Client Connected".encode())
while True:
if automationhat.input.one.is_on():
client.send("Input 1 On".encode())
time.sleep(0.1)
if automationhat.input.one.is_off():
client.send("Input 1 Off".encode())
if automationhat.input.two.is_on():
client.send("Input 2 On".encode())
time.sleep(0.1)
if automationhat.input.two.is_off():
client.send("Input 2 Off".encode())
if automationhat.input.three.is_on():
client.send("Input 3 On".encode())
time.sleep(0.1)
if automationhat.input.three.is_off():
client.send("Input 3 Off".encode())
#Handle Server being disconnected.
try:
client.send("Connect Test!".encode())
except:
print ("Disconnecting")
client.close()
print ("Disconnected")
while True:
try:
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((host,port))
print ("Connected")
client.send("Client Connected".encode())
break
except Exception as e:
time.sleep(1)
client.close()