Ticket #467: server.2.py

File server.2.py, 2.1 KB (added by jck, 18 months ago)

wsgi server with the same functonalyty as the django gateway

Line 
1# Copyright (c) 2007-2009 The PyAMF Project.
2# See LICENSE for details.
3
4"""
5ByteArray test server.
6"""
7
8from pyamf.amf3 import ByteArray
9from pyamf import register_class
10import os, os.path
11
12
13class Example(object):
14  def  __init__(self):
15      self.id = 1
16      self.data = ByteArray()
17
18
19def getExample():
20    return Example()
21
22def echoExample(example):
23    return example
24
25register_class(Example, 'Example')
26
27
28def app(environ, start_response):
29            if environ['PATH_INFO'] == '/crossdomain.xml':
30                fn = os.path.join(os.getcwd(), os.path.dirname(__file__),
31                    'crossdomain.xml')
32   
33                fp = open(fn, 'rt')
34                buffer = fp.readlines()
35                fp.close()
36   
37                start_response('200 OK', [
38                    ('Content-Type', 'application/xml'),
39                    ('Content-Length', str(len(''.join(buffer))))
40                ])
41   
42                return buffer
43   
44            return gw(environ, start_response)
45
46services = {
47    'get': getExample,
48    'echo': echoExample
49}
50
51if __name__ == '__main__':
52    from pyamf.remoting.gateway.wsgi import WSGIGateway
53    from wsgiref import simple_server
54   
55    gw = WSGIGateway(services)
56   
57    def app(environ, start_response):
58            if environ['PATH_INFO'] == '/crossdomain.xml':
59                fn = os.path.join(os.getcwd(), os.path.dirname(__file__),
60                    'crossdomain.xml')
61   
62                fp = open(fn, 'rt')
63                buffer = fp.readlines()
64                fp.close()
65   
66                start_response('200 OK', [
67                    ('Content-Type', 'application/xml'),
68                    ('Content-Length', str(len(''.join(buffer))))
69                ])
70   
71                return buffer
72   
73            return gw(environ, start_response)
74
75   
76
77    httpd = simple_server.WSGIServer(
78        ('localhost', 8000),
79        simple_server.WSGIRequestHandler,
80    )
81
82    httpd.set_app(app)
83
84    print "Running ByteArray Test AMF gateway on http://localhost:8000"
85
86    try:
87        httpd.serve_forever()
88    except KeyboardInterrupt:
89        pass