File: soapware.py

package info (click to toggle)
python-soappy 0.9.7-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 500 kB
  • ctags: 1,020
  • sloc: python: 7,572; makefile: 43; sh: 16
file content (118 lines) | stat: -rwxr-xr-x 3,249 bytes parent folder | download
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python

# This server validates as of 4/23/01 when run with UserLand's SOAP validator
# (http://validator.soapware.org/).

import getopt
import sys

sys.path.insert (1, '..')

import SOAP

ident = '$Id: soapware.py,v 1.1.1.1 2001/06/27 21:36:14 cullman Exp $'

def whichToolkit ():
    return SOAP.SOAPUserAgent ()

def countTheEntities (s):
    counts = {'ctLeftAngleBrackets': 0, 'ctRightAngleBrackets': 0,
        'ctAmpersands': 0, 'ctApostrophes': 0, 'ctQuotes': 0}

    for i in s:
        if i == '<':
            counts['ctLeftAngleBrackets'] += 1
        elif i == '>':
            counts['ctRightAngleBrackets'] += 1
        elif i == '&':
            counts['ctAmpersands'] += 1
        elif i == "'":
            counts['ctApostrophes'] += 1
        elif i == '"':
            counts['ctQuotes'] += 1

    return counts

def easyStructTest (stooges):
    return stooges['larry'] + stooges['moe'] + stooges['curly']

def echoStructTest (myStruct):
    return myStruct

def manyTypesTest (num, bool, state, doub, dat, bin):
    return [num, SOAP.booleanType (bool), state, doub,
        SOAP.dateTimeType (dat), bin]

def moderateSizeArrayCheck (myArray):
    return myArray[0] + myArray[-1]

def nestedStructTest (myStruct):
    return easyStructTest (myStruct.year2000.month04.day01)

def simpleStructReturnTest (myNumber):
    return {'times10': myNumber * 10, 'times100': myNumber * 100,
        'times1000': myNumber * 1000}

namespace = 'http://www.soapware.org/'

DEFAULT_HOST    = 'localhost'
DEFAULT_PORT    = 8080

def usage (error = None):
    sys.stdout = sys.stderr

    if error != None:
        print error

    print """usage: %s [options]
  If a long option shows an argument is mandatory, it's mandatory for the
  equivalent short option also. The default (if any) is shown in brackets.

  -?, --help            display this usage
  -h, --host=HOST       use HOST in the address to listen on [%s]
  -p, --port=PORT       listen on PORT [%d]
""" % (sys.argv[0], DEFAULT_HOST, DEFAULT_PORT),

    sys.exit (0)

def main ():
    host = DEFAULT_HOST
    port = DEFAULT_PORT

    try:
        opts, args = getopt.getopt (sys.argv[1:], '?h:p:',
            ['help', 'host', 'port'])

        for opt, arg in opts:
            if opt in ('-?', '--help'):
                usage ()
            elif opt in ('-h', '--host'):
                host = arg
            elif opt in ('-p', '--port'):
                port = int (arg)
            else:
                raise AttributeError, \
                     "Recognized but unimplemented option `%s'" % opt
    except SystemExit:
        raise
    except:
        usage (sys.exc_info ()[1])

    server = SOAP.SOAPServer ((host, port))

    server.registerFunction (whichToolkit, namespace)
    server.registerFunction (countTheEntities)
    server.registerFunction (easyStructTest)
    server.registerFunction (echoStructTest)
    server.registerFunction (manyTypesTest)
    server.registerFunction (moderateSizeArrayCheck)
    server.registerFunction (nestedStructTest)
    server.registerFunction (simpleStructReturnTest)

    server.serve_forever()

if __name__ == '__main__':
    try:
        sys.exit (main ())
    except KeyboardInterrupt:
        sys.exit (0)