I am working with raspberryPi 3 and I have a python script on it to receive connection requests as a tcp server..I need it to keep listening to the pins in case I receive any signal from them..I have done the code but it did not work as I want..maybe you can help me with an idea..here is my code
import socket
import time
from time import sleep
import RPi.GPIO as GPIO
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('10.1.39.117', 5040)
print('starting up on %s port %s' % server_address)
sock.bind(server_address)
Msg = "IamHere"
d = ""
sock.listen(1)
GPIO.setmode(GPIO.BOARD)
GatePin       = 11 #pin11 to open gate
closeGatePin  = 23 #pin23 to close gate
LoopsensorPin = 16 #pin16 for loop sensor
TngPin        = 22 #pin22 for tng signal
RFIDPin       = 32 #pin32 for RFID signal
Count         = 1
Status = "closed"
GPIO.setwarnings(False)
def OpenGate():
    print ("Opening Gate.. led on....")
    GPIO.output(GatePin, GPIO.HIGH)  # led on
    time.sleep(0.5)
def CloseGate():
    print ("Closing Gate..led off...")
    GPIO.output(GatePin, GPIO.LOW)  # led off
    time.sleep(0.5)
def setup():
    GPIO.setup(GatePin, GPIO.OUT)  # Set GatePin's mode is output
    GPIO.output(GatePin, GPIO.LOW)  # Set GatePin high to off led
    GPIO.setup(closeGatePin, GPIO.OUT)
    GPIO.output(closeGatePin, GPIO.LOW)
GPIO.setup(LoopsensorPin,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(TngPin,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(RFIDPin,GPIO.IN,pull_up_down=GPIO.PUD_UP)
setup()
print("Start..")
while(True):
    if GPIO.input(LoopsensorPin)==0:
        print("Signal received from main loop sensor ")
        CloseGate()
        Count = 0
        Status = "closed"
        print("Gate status is: "+Status)
    #Listen for incoming connections
    #Wait for a connection
    print('waiting for a connection')
    connection, client_address = sock.accept()
    try:
        print('connection from', client_address)
        #while True:
        data = connection.recv(16)
        d = data.decode('utf-8')
        print('received from agent: %s' % d)
        if d == 'Open':
            print('sending response back to the agent')
            #connection.sendall(data)
            connection.send(str(Msg).encode())
            #open gate
            OpenGate()
            Status = "opened"
            print("Gate status is: "+Status)
            print("Waiting for loop sensor signal..")
            GPIO.input(LoopsensorPin)
            while(1):
                if GPIO.input(LoopsensorPin)==0:
                    print("Signal received from loop sensor..")
                    print("loop sensor pressed")
                    sleep(.5)
                    CloseGate()
                    break
    finally:
        # Clean up the connection
        connection.close()
GPIO.cleanup()
I think I need to change something in the While loop to make it work but I am not sure how..
After I put all the tcp code in a thread, here is my new While loop:
print("Application Start..")
if __name__ == "__main__":    
    myTCPThread = TCPThread(shared_variable)
    myTCPThread.start()
while(True):
    if GPIO.input(LoopsensorPin)==0:
        print("Signal received from main loop sensor ")
        CloseGate()
        Count = 0
        Status = "closed"
        print("Gate status is: "+Status)
    if shared_variable == 'Open':
        print('sending response back to the agent')
        connection.send(str(Msg).encode())
        #open gate
        OpenGate()
        Status = "opened"
        print("Gate status is: "+Status)
        print("Waiting for loop sensor signal..")
        GPIO.input(LoopsensorPin)
        while(1):
            if GPIO.input(LoopsensorPin)==0:
                print("Signal received from loop sensor..")
                print("loop sensor pressed")
                sleep(.5)
                CloseGate()
                break           
myTCPThread.close()
myTCPThread.join()      
GPIO.cleanup()  
 
    