Skip to content
Snippets Groups Projects

Hangar door simulator

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Jörn Roth

    This simulates the operations of the hangar door TCP PLC actuator by listening to 127.0.0.1:5000 and replying to incoming queries with opened or closed

    Edited
    door_sim.py 1.26 KiB
    import socket
    import time
    
    '''
    This simulates the operations of the hangar door TCP PLC actuator by listening to 127.0.0.1:5000 and replying to 
    incoming queries with `opened` or `closed`
    '''
    
    
    def main():
        address = "127.0.0.1"
        port = 5000
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.bind((address, port))
        print("Simulating hangar TCP server on %s:%d" % (address, port))
        sock.listen()
        while True:
            conn, addr = sock.accept()
            print("connection established from", addr)
            while True:
                try:
                    rx = conn.recv(512)
                    if len(rx) > 0:
                        print(" ->", rx)
                        time.sleep(5)
                        response = b"OK"
                        if 'open' in str(rx).lower():
                            response = b"opened"
                        elif 'close' in str(rx).lower():
                            response = b"closed"
                        print(" <- " + response.decode("utf-8"))
                        conn.send(response)
                        conn.close()
                        print("Connection closed")
                        break
                except ConnectionAbortedError:
                    print("Connection aborted")
                    break
    
    
    if __name__ == '__main__':
        main()
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment