Spam Server help needed

Chris chris.rowley at
Tue May 23 20:29:26 CEST 2000

I am a final year university degree student trying to implement a simple
com/corba bridge using Python.  At the moment I am having problems building
and calling any Python COM code.  Even code which other people have run with
no problems is causing me problems.

Are you familiar with the spam server?

I have tried running it with no argument and everything appears ok.  When I
run with /test as an argument i get this response.  My final year project is
due in on Thursday.  if you could point me in the right direction I would
really appreciate it.

This is the interactive window after running spamserver first with no
argument and then with "/test" as an argument.

>>> Registered: Python.SpamServerGPO
doing direct tests...
Created spam server
added 2 cheese sandwiches
added some strong beer
contains 3 items:
 3 cans of spam
 2 Sandwichs of Cheese
 6 Bottles of Chimay Rouge
ditched the beer, we are at work here!
Tests passed
testing COM
**** - The Python.SpamServer test server is not available

Spam server code

# copyright Andy Robinson 1997
# you may freely modify and reuse this under the same terms
# as the main python licence

# registration stuff modified and expanded by Harri Pasanen
# thanks also to Mark Hammond and Greg Stein

# modified 7/8/99 to demonstrate having all clients on a
# machine work with the same SpamServer data.
# Gordon McMillan

from win32com.server.exception import COMException
import win32com.server.util
import win32com.client.dynamic
import sys
import pythoncom

_cans = None

class SpamServer:
    _reg_clsid_ = "{EC419091-35FC-11D3-9403-00609736B700}"
    _reg_desc_ = "Python SpamServer GPO"
    _reg_progid_ = "Python.SpamServerGPO"
    _reg_class_spec_ = "spamserver_GPO.SpamServer"
    _reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER
    _public_methods_ = ['AddCan',
    # you need to explicitly list the public methods for COM to work
    # spelling errors here lead to errors when you call CreateObject
    # from a client

    def __init__(self):
        "it keeps a collection of data - start with a sample object"
        global _cans
        if _cans is None:
            _cans = [SpamCan()]
        self.cans = _cans

    def GetCanCount(self):
        "return the number of objects held"
        return len(self.cans)

    def AddCan(self, contents = 'Spam', type = 'Can', qty = 3):
        "two uses, can just add a generic one, or one you created in a GUI"
        newOne = SpamCan()
        newOne.contents = contents
        newOne.type = type
        newOne.qty = qty

    def DeleteCan(self,index):
        del self.cans[index]

    def GetCanAt(self, index):
        return win32com.server.util.wrap(self.cans[index])

    def GetDescriptionList(self):
            # for a list view
        return map(lambda x:x.GetDescription(), self.cans)

    def GetCrosstab(self):
            # some example data for yout to play with
        return [['e.g.','cans','bottles','bags'],

class SpamCan:
    "just a simple 'packet' of data to play with"
    _public_methods_ = ['GetDescription']
    _public_attrs_ = ['contents','type','qty']

    def __init__(self):
        self.contents = 'spam'
        self.type = 'can'
        self.qty = 3

    def GetDescription(self):
        return '%d %ss of %s' %(self.qty, self.type, self.contents)

# each class needs a class ID.  You can get these by typing:
# import pythoncom
# g = pythoncom.CreateGuid()
# print g
# in the interactive window, then pasting it into your code.

def RegisterSpam():
    # register them both - this must be called once for the
    # demo to work
    import win32com.server.register

def UnRegisterSpam():
    """ Unregister each server - use this before
    deleting the server to keep your registr tidy"""

    print "Unregistering COM server..."
    from win32com.server.register import UnregisterServer
    UnregisterServer(SpamServer._reg_clsid_ ,
    print "SpamServer Class unregistered."

    # and finally some test code

def TestDirect():
    # does some stuff to verify the class works IN PYTHON
    s = SpamServer()
    print 'Created spam server'
    s.AddCan('Cheese', 'Sandwich', 2)
    print 'added 2 cheese sandwiches'
    s.AddCan('Chimay Rouge', 'Bottle', 6)
    print 'added some strong beer'
    print 'contains %d items:' % (s.GetCanCount())
    for item in s.GetDescriptionList():
        print '\t' + item
    print 'ditched the beer, we are at work here!'
    print 'Tests passed'

def TestCOM():
        SpamSrv = win32com.client.dynamic.Dispatch("Python.SpamServerGPO")
        print 'Python.SpamServerGPO class created from COM'
        print "**** - The Python.SpamServer test server is not available"
    print 'cans:', SpamSrv.GetCanCount()
    print 'Adding a cheese sandwich', SpamSrv.AddCan('Cheese', 'Sandwich',
    print 'Adding some beer:', SpamSrv.AddCan('Chimay Rouge', 'Bottle', 6)
    print 'cans: ', SpamSrv.GetCanCount()
    descr = SpamSrv.GetDescriptionList()
    for item in descr:
        print item
    print "The Python.Spamserver COM Server worked OK."

if __name__=='__main__':
    """If they ran the whole script, register classes.
    Options also given to unregister and to test"""
    import sys
    if "/unreg" in sys.argv:
    elif "/test" in sys.argv:
        print "doing direct tests..."
        print "testing COM"

Chris Rowley

More information about the Python-list mailing list