What would be the best way to run python client in the background

gel geli at tasmail.com
Tue Aug 22 01:26:26 EDT 2006


Tim Golden wrote:

> | Tim Golden wrote:
> |
> | > [gel]
> | >
> | > | I have written a python client server app [...]
> | > | I want to run the client end of the app more or less invisibly
> | > | (no console) on the XP clients when ever a users logs on.
>
> [Tim G]
> | > The normal way is to run a Win32 service. There are several
> | > posts on the subject around the Python and Python-Win32
> | > mailing lists / newsgroups. The alternative is to set something
> | > to run when any user logs in to run without a window. Again,
> | > there are posts on the subject, including one I seem to remember
> | > from Robin Becker, which tell how to do this kind of thing.
>
> [gel]
> | Yes running the client as a service seems to be the way to go.  I have
> | had a bit of a look at the py2exe way of doing it, but have not got my
> | head around it yet.  What would be your preferred way of creating a
> | service for an XP client?
>
> I've always coded pretty much from scratch, reusing a tried-and-trusted
> skeleton and fitting things in. That said, I've never had to put
> together
> a service that was anything more than lightweight. The attached template
> I
> culled from someone's webpage, can't remember where I'm afraid. I
> believe
> people have had success in using py2exe to create a service but I
> haven't
> tried it myself.
>
> Hopefully more experienced service-writers will chime in with coherent
> advice. You might also try the python-win32 mailing list, since not
> everyone who reads that reads this I imagine.
>
> TJG
>
Can I give you a look at the client side and ask for a suggestion as
how best to plug this into the template that you have supplied

The attempts that I made failed to start without much in the way of
error messages.

Any suggestion to do with my python style that you have would be
appreciated too.


# Import needed modules
import EasyDialogs
import wmi
import pythoncom
import threading
import time
import Pyro.core
import Pyro.naming, Pyro.core
from Pyro.errors import NamingError



# locate the NS
locator = Pyro.naming.NameServerLocator()
print 'Searching Name Server...',
ns = locator.getNS()

# resolve the Pyro object
print 'finding object'
try:
    URI=ns.resolve('test')
    print 'URI:',URI
except NamingError,x:
    print 'Couldn\'t find object, nameserver says:',x
    raise SystemExit

# make and instance of Pyro object
o = Pyro.core.getProxyForURI(URI)

#instance of wmi and get mac address of enabled NIC
c = wmi.WMI()
for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1):
#  print interface.Description, interface.MACAddress
  for ip_address in interface.IPAddress:
     mac = interface.MACAddress
# put current time into var current_time
colItems = c.ExecQuery("SELECT UserName FROM Win32_ComputerSystem")
for objItem in colItems:

    if objItem.UserName != None:
        usr_name = str(objItem.UserName)
        print "UserName: " + usr_name


# assign the dictionary object that o.r_l() returns to
d_licence_numbers
d_licence_numbers = o.r_l()
# So I can see how many licences are available for each piece of
software print d_licence_numbers
print d_licence_numbers

# Define the create class that watches for new processes to start
def diag(mesg):
        EasyDialogs.Message(mesg)

def create():
        global usr_name
        pythoncom.CoInitialize()
        c = wmi.WMI()
        while 1 :
            current_time = time.time()
            d_clnt = {'MAC':mac, 'time':current_time, 'usr_name':
usr_name}
            print d_clnt
            print "watching creation"
            watcher = c.watch_for(notification_type="Creation",
                                  wmi_class="Win32_Process",
                                   delay_secs=1)

            proc = watcher()
            name = proc.Name
            if d_licence_numbers.has_key(name):
                print "Process being watched = " + name

                if len(o.r_d()[name]) != 0:
                    li = o.r_d()[name]
                    registered = False
                    for i in li:
                        print usr_name
                        print i['usr_name']
                        if mac == i['MAC'] or i['usr_name'] ==
usr_name:
                            print "Already in there"
                            registered = True
                    if registered != True:
                        for i in li:
                            #ps_id = ps_li.index(sw)
                            if len(o.r_d()[name]) < o.r_l()[name]:

                                o.a_d(name, d_clnt)
                                print "There is a license avialable for
" + name
                                print o.r_d()

                            else:
                                print "There are no licenses avialable
for " + name


                                for process in c.Win32_Process ():
                                    if process.Name == name:
                                        print process.ProcessId,
process.Name
                                        result = process.Terminate ()
                                n = str(name)
                                mes =   'there are no copies of ' + n +
' available, try again later'


threading.Thread(target=diag(mes)).start()

                else:

                            o.a_d(name, d_clnt)
            else:
                print "Process " + name + " is not being watched"

# Define the delete() class that watches for processes stopped
def delete():
        global mac
        pythoncom.CoInitialize()
        d = wmi.WMI()
        while 1 :
            current_time = time.time()
            print "watching deletion"
            watcher = d.watch_for(notification_type="Deletion",
                                  wmi_class="Win32_Process",
                                  delay_secs=1)
            proc = watcher()
            name = proc.Name
            if d_licence_numbers.has_key(name):
                print "Process being watched = " + name
                still_running = False
                for process in d.Win32_Process ():
                    if process.Name == name:
                        still_running = True
                        print name + ' is still running '

                if still_running == False:
                    perm = False
                    li = o.r_d()[name]
                    for i in li:
                        if i['time'] != 0 and i['MAC'] == mac:
                            o.dd_d(name,mac)
                            print name + " licence is being released "
                            perm = False
                        else:
                            perm = True

                    if perm:
                        print "Not releasing licence - it is a
permenant licence"

            else:
                print "Process " + name + " not being watched"



# Start a thread for each create and delete classes to run in and watch
for processes starting and stopping
threading.Thread(target=create).start()
threading.Thread(target=delete).start()




More information about the Python-list mailing list