Beispiel #1
0
incoming.listen(3)

while True:
    (client, addr) = incoming.accept()

    header = Header(client.recv(8))
    print("Receiving {} bytes from {}".format(header.size, addr))

    buf = client.recv(header.size - 8)
    while len(buf) < header.size - 8:
        buf += client.recv(header.size - 8 - len(buf))

    liquid = Liquid(buf)
    
    liquid.treat_fungi()
    print("Found {} after fungus".format(len(liquid.hazmats)))

    # Find out if trash; 2 for data, 1 for l, 1 for right.
    # If L or R point to # > header.size-8 / 4; scrap packet.
    # Needs to use structs.  Not sure how.  Oh well.
    if(liquid.trash()):
        #Send packet to downstream.  Continue at while True.
        print("Trash Packet.")
        trsh_out = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        trsh_out.connect(("downstream", 2222))
        header.type = 1
        trsh_out.send(header.serialize())
        trsh_out.send(buf)
        trsh_out.close()
        client.close()