So I am doing a project at work, where I am communicating with an external IO-Module PCB, over a TCP connection. So essentially a GUI, talking to an embedded system, via TCP. I then want to add a command, where I can ask the PCB do reboot, and automatically reconnect with the PCB when this is done. I have snipped out the sections of the code that is relevant to my question (it is supposed to be way bigger, but this section should illustrate the problem) - please dont look to much into the "reboot signal" function, this only makes sense to you if you have the full understanding of the external PCB and how that works (think of it as a signal, that reboots the PCB, nothing else)
My thought is that i need some kind of "socket.forget" function, I am not sure I am correct in this assumption. As you can see in the code, I am calling a "socket.close" function, and want to then re-use the same function as i connected with in the first place. But that gives me an "AttributeError: 'socket' object has no attribute 'socket'" error.
import socket
import time
global socket
def connect():
global socket
connected = 0
socket = socket.socket()
try:
socket.bind(("192.168.0.10", 2333))
except:
print("connection failed")
else:
print("connecting")
for x in range(0, 2):
if connected == 0:
adress = '192.168.0.240'
server_address = (adress, 2333)
print('starting up on {} port {}'.format(*server_address))
try:
socket.connect(server_address)
except:
print("trying again")
else:
print("connection successfully established")
connected = 1
if connected == 0:
print("connection failed")
def reboot_signal():
filler_byte = 0
reboot = 1
checksum = 1
socket.send(bytes([filler_byte, checksum, filler_byte, reboot, filler_byte,filler_byte ,filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte, filler_byte,filler_byte]))
def reboot_PCB():
global socket
reboot_signal() # dont worry about this, this is essentially just a TCP signal, rebooting the PCB I am communicating with
print('rebooting')
socket.close()
time.sleep(10) # Now I am giving the PCB some time to reboot
connect() # now i want to re-connect
connect()
time.sleep(5) # Now I am giving the PCB some time to reboot
a = 1
#my alternative way of making and endless loop
while( a == 1):
reboot_PCB()