source: cmfdeployment/branches/kenw-p4-wip/lib/policycaller.py @ 2380

Last change on this file since 2380 was 2380, checked in by kenw, 8 years ago

Committing new branch intended for Plone 4 compatibility; mostly required imports of new zope packages

File size: 2.3 KB
Line 
1#!/usr/bin/env python2.1
2"""
3
4Python script meant to be called via cron which uses httplib to call
5the garbage collection method on a session data manager (for use when
6synchronous garbage collection is not employed.
7
8"""
9
10import sys, httplib, string, base64, getopt, traceback, time
11
12def method_caller(server, method, user, password):
13    h = httplib.HTTP(server)
14    h.putrequest('GET', method)
15    h.putheader('User-Agent', 'Zope method caller')
16    h.putheader('Accept', 'text/html')
17    h.putheader('Accept', 'text/plain')
18    h.putheader('Host', server)
19    if user and password:
20        h.putheader("AUTHORIZATION", "Basic %s" % string.replace(
21            base64.encodestring("%s:%s" % (user, password)),
22            "\012", ""))
23    h.endheaders()
24    errcode, errmsg, headers = h.getreply()
25    if errcode != 200:
26        f = h.getfile()
27        data = f.read()
28        f.close()
29        print data
30
31if __name__ == '__main__':
32    usage = ("Usage:\n%s -s servername:port -m methodname -u username "
33             "-p password -c continuous(secs)\n%s -h (prints this help)\n\n"
34             % (sys.argv[0], sys.argv[0]))
35    opts, args = getopt.getopt(sys.argv[1:], 's:m:u:p:c:h')
36    server = None
37    method = None
38    user = None
39    password = None
40    period = None
41    for o, a in opts:
42        if o == '-s':
43            server = a
44        if o == '-m':
45            method = a
46            if method[0] != '/':
47                method = '/' + method
48            if method[-1] == '/':
49                method = method[:-1]
50        if o == '-u':
51            user = a
52        if o == '-p':
53            password = a
54        if o == '-h':
55            print usage
56            sys.exit()
57        if o == '-c':
58            period = int(a.strip())
59    if not server:
60        print usage + 'Error: need a server name via -s'
61        sys.exit(1)
62    elif not method:
63        print usage + 'Error: need a method name via -m, e.g. /getnews'
64        sys.exit(1)
65    elif not user:
66        print usage + 'Error: need a user name via -u'
67        sys.exit(1)
68    elif not password:
69        print usage + 'Error: need a password via -p'
70        sys.exit(1)
71
72    while 1:
73        try:
74            method_caller(server, method, user, password)
75        except:
76            traceback.print_exc()
77        if period is None: break
78        time.sleep(period)
79
80           
Note: See TracBrowser for help on using the repository browser.