Win32 Read Registry Tag

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from django import template
import _winreg, os.path

register = template.Library()

# Define the full path to the registry location 
# id = PATH\stringvalue
#
# Only expose a few settings...The alternative is to pass this as a parameter from the template.
str_paths = dict( 
    computername = r'SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName\ComputerName',
    spamcount = r'SOFTWARE\Spam\CurrentNumberOfEggs',
    eggcount = r'SOFTWARE\Spam\CurrentNumberOfSpam'
)

@register.filter
def registry( server, reg ):
    '''
    Example usages:
        {% server|registry:"computername" %} to query server's registry setting for str_paths['computername'].
        {% server|registry:"spamcount" %} to query server's registry setting for str_paths['spamcount'].
        {% server|registry:"eggcount" %} to query server's registry setting for str_paths['eggcount'].
    
    '''
    conn, regkey, val = None, None, ('','')
    try:
        regpath, var = os.path.split( str_paths[reg] )
        conn = _winreg.ConnectRegistry(r'\\%s' % server, _winreg.HKEY_LOCAL_MACHINE)
        regkey = _winreg.OpenKey( conn, regpath )
        val = _winreg.QueryValueEx(regkey, var)
    except Exception, e:
        pass
        
    if conn: _winreg.CloseKey( conn )
    if regkey: _winreg.CloseKey( regkey )
    
    return val[0]

More like this

  1. automating twitter by lawgon 4 years, 9 months ago
  2. Dynamical formsets by I159 2 years, 5 months ago
  3. Overwrite some views in settings.py by guettli 5 years ago
  4. PropertyBasedContext by a4tunado 2 years, 9 months ago
  5. Plaintext password by yetty 5 months, 3 weeks ago

Comments

mpa (on March 11, 2007):

You have to modify the str_paths dictinary to point to meaningful registry paths. The last item on the path should resolve to the REG_SZ key-variable to query.

#

(Forgotten your password?)