testDCOM.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # testDCOM
  2. usage="""\
  3. testDCOM.py - Simple DCOM test
  4. Usage: testDCOM.py serverName
  5. Attempts to start the Python.Interpreter object on the named machine,
  6. and checks that the object is indeed running remotely.
  7. Requires the named server be configured to run DCOM (using dcomcnfg.exe),
  8. and the Python.Interpreter object installed and registered on that machine.
  9. The Python.Interpreter object must be installed on the local machine,
  10. but no special DCOM configuration should be necessary.
  11. """
  12. # NOTE: If you configured the object locally using dcomcnfg, you could
  13. # simple use Dispatch rather than DispatchEx.
  14. import pythoncom, win32com.client, win32api, string, sys
  15. def test(serverName):
  16. if string.lower(serverName)==string.lower(win32api.GetComputerName()):
  17. print("You must specify a remote server name, not the local machine!")
  18. return
  19. # Hack to overcome a DCOM limitation. As the Python.Interpreter object
  20. # is probably installed locally as an InProc object, DCOM seems to ignore
  21. # all settings, and use the local object.
  22. clsctx = pythoncom.CLSCTX_SERVER & ~pythoncom.CLSCTX_INPROC_SERVER
  23. ob = win32com.client.DispatchEx("Python.Interpreter", serverName, clsctx=clsctx)
  24. ob.Exec("import win32api")
  25. actualName = ob.Eval("win32api.GetComputerName()")
  26. if string.lower(serverName) != string.lower(actualName):
  27. print("Error: The object created on server '%s' reported its name as '%s'" % (serverName, actualName))
  28. else:
  29. print("Object created and tested OK on server '%s'" % serverName)
  30. if __name__=='__main__':
  31. if len(sys.argv) == 2:
  32. test(sys.argv[1])
  33. else:
  34. print(usage)