2

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()
2

I rewrote my code so the connection is make when needed. which fix a few other problems for me.

Only needed to rewrite the client side code:

#!/usr/bin/env python3

import automationhat
import time
import socket, sys

def sendMessage(msg):
    try:
        print(msg)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        host = "123.456.789.123"
        port = 12345
        s.connect((host, port))
        s.send(msg.encode())
        print("Message sent")
        from_server = s.recv(4096)
        s.close()
    except:
        print(sys.exc_info()[0])

sendMessage("Client Connected")

while True:
    if automationhat.input.one.is_on():
        sendMessage("Input 1 On")
        time.sleep(0.1)
        if automationhat.input.one.is_off():
            sendMessage("Input 1 Off")

    if automationhat.input.two.is_on():
        sendMessage("Input 2 On")
        time.sleep(0.1)
        if automationhat.input.two.is_off():
            sendMessage("Input 2 Off")

    if automationhat.input.three.is_on():
        sendMessage("Input 3 On")
        time.sleep(0.1)
        if automationhat.input.three.is_off():
            sendMessage("Input 3 Off")
  • Please accept your answer with a click on the tick on the left side. This will finish the question and it will not pop up again after months. – Ingo Nov 25 '19 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.