#!/usr/bin/python #This script is the fake heartbeat script that was originally set up as a #static test and an integration tool. However, this script is being modified #to be python 2.5 compatible, and to separate the functions of packet storage #and information scraping. #Python imports. socket is a python system package, and bson is provided #as a folder containing the bson module. import socket import bson #Default values for testing. NOTE: The code below uses these static values! #This should definitely be changed in the future. DEFAULT_IP="127.0.0.1" DEFAULT_PORT=8123 # This function is where information is packed into pack_data. The data is # stored in a python dict that is eventually BSON-ified. However, this function # is solely responsible for populating pack_data. def stuff_heart(pack_data): #test that fields in the packet exist. TODO: Make these much better tests. print "--testing key access..." print pack_data["smac"] print pack_data["sip"] print pack_data["path"] #At this point, all necessary fields exist. #Sample scrape. pack_data["smac"] = "DE:AD:BE:EF:CA:FE" pack_data["sip"] = 623 #Path information will be reported in source order! pack_data["path"] = ["DE:AD:BE:EF:CA:00", "DE:AD:BE:EF:CA:01", "DE:AD:BE:EF:CA:02"] # This is the main function. Details are inline. def main(): #Initialization of empty data set to scrape into, and then immediately #stuffed wit information. data = {"smac":"", "sip":-1, "path":[]} stuff_heart(data) #Stuff a BSON representation into packet_data. Also debug prints. TODO: Add #a verbose flag for the debug prints. packet_data = bson.dumps(data) print packet_data transmitting = bson.loads(packet_data) print transmitting sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(packet_data,(DEFAULT_IP, DEFAULT_PORT)) if __name__ == '__main__': main()