Code:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
'''
demo of pyBM API , must be launched from within pyBM.../src folder ;      see   http://bitmessage.mybb.im/viewtopic.php?id=33   and     https://github.com/Bitmessage/PyBitmessage/wiki
'''
import subprocess
import xmlrpclib
import ntpath
import socket
import base64
import random
import json
import time
import sys
import api
import os
from bmconfigparser import BMConfigParser
import ConfigParser

hier = os.path.abspath(__file__)

def getApiString():
    global keysPath
        
    config = ConfigParser.SafeConfigParser()
    keysPath = 'keys.dat'
    config.read(keysPath) # first try to load keys.dat from source directory.

    try:
        config.get('bitmessagesettings','settingsversion')
        keysFolder = ''
    except:
        # keys.dat not found in source directory. Try appdata directory.
        keysFolder = findKeysFolder()
        keysPath   = keysFolder + 'keys.dat'
        config     = ConfigParser.SafeConfigParser()
        config.read(keysPath)

        try:
            config.get('bitmessagesettings','settingsversion')
        except:
            # keys.dat not there either. something is wrong.
            drawHeader()
            print ' '
            print '+--------------------------------------------------------------------+'
            print '|                 unable to access the Bitmessage keys.dat file.     |'
            print '|   Check to ensure this is in the same directory as Bitmessage.  |'
            print '+--------------------------------------------------------------------+'
            print ' '
            print config
            print ' '
    
    '''                     put this in manually into keys.dat
    
    apienabled = true
    apiport = 8442
    apiinterface = 127.0.0.1
    apiusername = bmf999
    apipassword = bmf999
    daemon = False
    dontconnect = True    
    
    '''

    apiPort =    '8442'
    try:         apiPort = config.getint('bitmessagesettings', 'apiport')
    except:      print '  ( error ) unable to access keys.dat'
        
    apiInterface = '127.0.0.1'    
    try:         apiInterface = config.get('bitmessagesettings', 'apiinterface')
    except:      print '  ( error ) unable to access keys.dat'
        
    apiUsername  = 'bm999'
    try:         apiUsername = config.get('bitmessagesettings', 'apiusername')
    except:      print '  ( error ) unable to access keys.dat'
      
    apiPassword  = 'bmf999'    
    try:         apiPassword = config.get('bitmessagesettings', 'apipassword')
    except:      print '  ( error ) unable to access keys.dat'

    apiString    = "http://" + apiUsername + ":" + apiPassword + "@" + apiInterface+ ":" + str(apiPort) + "/"
    return apiString

def apiTest(apicall):
    try:
        result = apicall.add(22,11)
        if   result ==       33:        
            pass #; print "bitmessage API is enabled and responding. proceeding."
        else               :        
            print "7 bitmessage API error. check keys.dat configuration. is bitmessage running?. exiting."
            sys.exit(7)
    except:                         
        print "8 bitmessage API error. check keys.dat configuration. is API enabled? is bitmessage running?. exiting."
        sys.exit(8)

    
def getAddress(passphrase,vNumber,sNumber):
    passphrase = passphrase.encode('base64')
    return api.getDeterministicAddress(passphrase,vNumber,sNumber)
    api.statusBar("getting chan address.")



def sendBM():
    address = getAddress("bitmessage", 4, 1)  # general
    
    timeStamp  = time.time()
    timeString = str(timeStamp)
    timeSecs   = timeString[:10]    
    
    plainSubject = "  autopost this stuff " # + timeSecs
    print "( subject ) ", plainSubject
        
    plainMessage = " an api client "
    message = plainMessage.encode('base64')
    subject = plainSubject.encode('base64')
    ttl =  30  #3600
    try:
        ackData = api.sendMessage(address, address, subject, message, 2, ttl)
        print "( status  ) ", api.getStatus(ackData)                             # if err, chan is not joined
    except:
        print "( api ) API Error."
        

if __name__   == "__main__":    
    api_string =                    getApiString()    
    api        = xmlrpclib.ServerProxy( api_string)
    #print "api_string   is  ",         api_string      ##   api_string       http://bmf999:bmf999@127.0.0.1:8442/ 
    apiTest(api) ;     #print(hier)
    sendBM()