#!/usr/bin/python # initially written by Shane Anglin on 5-19-01 # JSA - 06-04-01 added multiple pager capability # JSA - 06-01-01 added module to replace non-friendly XML characters # JSA - 06-12-01 added more documentation comments # # Call this program from another program by using the following commands: # from wctpXml import * # pagers = [1111111,2222222,3333333] # msgString = 'This is a test message' # wctpxmlSendMsg(msgString,pagers) import os, sys, time, string, smtplib, httplib from Pager import * def sendFailureEmail(String): sndStringX = String separatorline = '------------------------' server=smtplib.SMTP("smtp.yourdomain.com") tolist=["person1@yourdomain.com"] msg = "From: AutoNotify@yourdomain.com\012Subject: Message Failed for WCTP SendMsg" + "\012\012" + "The following is the message that failed for the WCTP SendMsg: \012\012" + separatorline + "\012" + sndStringX + separatorline + "\012\012" fromwhom = "autonotify@yourdomain.com" server.sendmail(fromwhom,tolist,msg) server.quit() def replaceChars(msgText): # this module replaces non-friendly XML characters x = 0 replaceList = [('&','&'),('\033','<esc>'),('"','"'),("'",'''),('<','<'),('>','>')] for replacement in replaceList: toBeReplaced = replaceList[x][0] replaceWith = replaceList[x][1] msgText = string.replace(msgText,toBeReplaced,replaceWith) x = x + 1 return msgText def fileContents1(): partA = '' partB = '' partC = '' partD = ' ' partE = ' debug_level(1)] and [disable logging=debug_level(0)] h.putrequest("POST","/wctp") # on some WCTP gateways, the "/wctp" may need to be "/WCTP" h.putheader("Content-Type","text/xml") formDataLEN= str(len(formDataZ)) # determine the length of the entire XML form h.putheader("Content-Length",formDataLEN) h.putheader('Accept', 'text/html') h.putheader('Accept', 'text/plain') h.endheaders() h.send(formDataZ) # send wctpXML data errcode, errmsg, headers = h.getreply() # read responses that return from the WCTP server print "errcode = ",errcode # error code should be 200 print "errmsg = ",errmsg #print "headers = ",headers currentDate = time.strftime("%m-%d-%y",time.localtime(time.time())) # i.e. 06-05-01 currentTime = time.strftime("%H:%M",time.localtime(time.time())) # example: 1408 for 2:08pm print "Current Time = " + currentTime + " and current date = " + currentDate print 'messageSent to ' + pagerID + ' = \012' + msgPayload print "\012" f = h.getfile() response = f.read() # read the raw HTML return file #print response # print the raw HTML return file f.close() return response def wctpxmlSendMsg(msgString,pagerList): # THIS MODULE IS EXPECTING TO RECEIVE A TEXT STRING AND A LIST OF PAGERID(S) msgPayload = msgString pagerListX = pagerList if len(pagerListX) == 0: print "The pagerList was empty! Please put at least one pagerid in the list!" time.sleep(5) sys.exit() todaysDate = "" todaysTime = "" # sys.stderr = sys.stdout msgPayload = replaceChars(msgPayload) # replace non-compliant XML characters for pageridX in pagerListX: # these formats must be exactly as shown todaysTime = time.strftime("%H:%M:%S",time.localtime(time.time())) # example: 10:12:34 todaysDate= time.strftime("%Y-%m-%d",time.localtime(time.time())) # example:2001-01-01 randomidNum = time.strftime("%Y%m%d%H%M%S",time.localtime(time.time())) # example: 20010517165456 timeStamp = todaysDate + 'T' + todaysTime # piece together the components of the XML form to be transmitted formData = buildXMLFile(msgPayload,str(pageridX),timeStamp,randomidNum) # compile the XML form cnt = 0 while cnt < 5: # try at least 5 times to transmit the XML data to the WCTP target server try: responseResult = postXML(formData,msgPayload,str(pageridX)) break # get out of while loop... SUCCESS! My work is finished here. # NOTE: to handle error codes, simply place IF statements after the # responseResult variable and check for error codes and perform # whatever action upon finding them except: print '***** Sending of XML form to WCTP Server failed... I will try again! *****' pass cnt = cnt + 1 if cnt == 6: # if you are here... bad news... the function, postXML, failed 5 times in a row try: sendFailureEmail(formData) # send an email to someone to notify them # of the sendMsg failure print ' I generated a notificiation email.' except: print ' I cannot even generate an email to tell anyone WCTP Send Message failed!' time.sleep(30) pass if __name__ == '__main__': # dummyMsg = "This is the MAIN module" dummyVar = 0