Hangar door simulator
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
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()
Please register or sign in to comment