views:

138

answers:

1

I'm having some strange trouble with pamie: http://pamie.sourceforge.net/ .

I have written a script to do some port (25) forwarding based on a recepie that I found on the web, Here is the code that matters:

# forwardc2s(source, destination):    
#   forwards from client to server.
#   Tries to post the message to ICE.
def forwardc2s(source, destination):    
    string = ' '
    message = ''
    while string:
        string = source.recv(1024)
        if string:

            if string[:4] == 'DATA' or message <> '':     # Put the entire text of the email into a variable: message
                message = message + string

            destination.sendall(string)
        else:
            posttotracker(message)                            # post message to tracker.
            source.shutdown(socket.SHUT_RD)
            destination.shutdown(socket.SHUT_WR)

The 'posttotracker' function is not yet complete... all that it contains is:

def posttotracker(message):
    ie = PAMIE('http://google.com/')

This gives me an error as follows:

Unhandled exception in thread started by <function forwardc2s at 0x00E6C0B0>
Traceback (most recent call last):
  File "main.py", line 2398, in forwardc2s
    posttotracker(message)                            # post message to tracker.
  File "main.py", line 2420, in posttotracker
    ie = PAMIE('http://google.com/')
  File "main.py", line 58, in __init__
    self._ie = win32com.client.dynamic.Dispatch('InternetExplorer.Application')
  File "c:\Python26\lib\site-packages\win32com\client\dynamic.py", line 112, in
Dispatch
    IDispatch, userName = _GetGoodDispatchAndUserName(IDispatch,userName,clsctx)

  File "c:\Python26\lib\site-packages\win32com\client\dynamic.py", line 104, in
_GetGoodDispatchAndUserName
    return (_GetGoodDispatch(IDispatch, clsctx), userName)
  File "c:\Python26\lib\site-packages\win32com\client\dynamic.py", line 84, in _
GetGoodDispatch
    IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.II
D_IDispatch)
pywintypes.com_error: (-2147221008, 'CoInitialize has not been called.', None, N
one)

The funny thing is that if I do the very same thing outside of this function (in the main function for example) the library works exactly as expected.

Any ideas?

Pardon me if this information is not sufficient, I'm just a starting python coder.

A: 

The PAMIE object does not work within threads!!!

I was originally starting forwardc2s as a thread. When I just call it as a function instead, everything works fine!

Please consider this question resolved... with great thanks to the rubber duck.

Alterlife