[python-win32] problem with wmi within a service

Boris Perez Canedo inf200523 at ucf.edu.cu
Tue May 25 15:38:56 CEST 2010



Hi everybody,

I'm having a problem when I try to query a remote (only a
remote )computer using wmi. The python code has been compiled to become a windows
service.

I'm planning to query systems that implement the DMTF standard,
WBEM, and for that I need a class that can handle cim-xml protocol and wmi
connections.

The functionalities are accessed through a XML-RPC server which
is run in a separated thread inside the SvcDoRun method. This server has a procedure
called Query that is used, obviously, to querie a remote systems or the local system.
Inside this procedure I do the pythoncom.CoInitialize and pythoncom.CoUninitialize and
in between I use the conection class mentioned above.

The service work
perfect when I query localhost but fails to query any remote machine. I have tried
wmitester.exe a tool to test wmi and using the same user and password and remote 
machine it works so It is not a permission problem.

I also changed the name
of the module wmi to my_wmi in case there could be a problem with wmi.dll but nothing
changes. The exception is here (__init__ at WBEMConn):

...
self.conn = None
...
try:
    self.conn =
wmi.WMI(computer = self.host, user = self.user, password = self.password)
    self.system = "wmi"
except wmi.x_access_denied:
    self.status = "access_denied"
    return


I have a module for the connection class
(It has been tested outside the service and it works).

import pywbem
import wmi

class WBEMConn:
    def __init__(self, url,
user, password)
          self.url =
url
          self.user = user
          self.passwd = password
          self.conn = None
          #--------Here the is attempted
a connection to a system which might be using cim-xml protocol
          #--------If fails try wmi.
          #--------if fails can not
talk cim-xml or wmi
          
          
    def ExecQuery(self,query):
         if self.conn is None:
               
return None
         if self.system ==
"cim-xml":
              result
= self.conn.ExecQuery("wql", query)#----for pywbem
         else:
              
result  = self.conn.query(query)    #---for wmi
               

A module that defines a class for the xml-rpc server.

class
ThreadedXMLRPCServer_BasicAuth(ThreadingMixIn, SimpleXMLRPCServer)

class
ServerFunc:
      def Query(self, url, user, password,
query):
              try:
                 
pythoncom.CoInitialize()
                         conn
= WBEMConn(url, user, password)
                        
result = conn.ExecQuery(query)
              
finally:
                       
pythoncom.CoUninitialize()
               return
result                  


The service's SvcDoRun method.

def SvcDoRun(self):
    #-----import all requiered modules
     def StartServer():
          
self.server.serve_forever()
     self.server =
ThreadedXMLRPCServer_BasicAuth(...)
     func = ServerFunc()
     self.server.register_instance(func)
     StartServer()
     while
self.running:
          time.sleep(3)
def
SvcStop(self):
   ......
   self.server.shutdown()
   self.running = False
  .....
    

Thanks a lot,
Boris.









---
Convencion Internacional de Psicologia y Ciencias Sociales y Humanas
Cienfuegos, Cuba, del 19 al 22 de abril del 2011
http://promociondeeventos.sld.cu/hominis2011
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20100525/ca0026d5/attachment.html>


More information about the python-win32 mailing list