-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·78 lines (57 loc) · 2.21 KB
/
server.py
File metadata and controls
executable file
·78 lines (57 loc) · 2.21 KB
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
#!/usr/bin/env python
import logging
logging.basicConfig(level=logging.DEBUG)
from spyne import Application, rpc, ServiceBase, \
Integer, Unicode, Enum, Fault, InvalidCredentialsError
from spyne import Iterable
from spyne.protocol.soap import Soap11
from spyne.server.wsgi import WsgiApplication
part_of_day = Enum("morning", "afternoon", "evening", "night", type_name="PartOfDay")
class HelloWorldService(ServiceBase):
@rpc(Unicode, Integer, _returns=Iterable(Unicode, min_occurs=1))
def say_hello(ctx, name, times):
for i in range(times):
yield 'Hello, %s' % name
@rpc()
def say_nothing(ctx):
return
@rpc(part_of_day, _returns=Unicode)
def greet(ctx, part_of_day):
return 'Good %s' % part_of_day
@rpc(Iterable(part_of_day, min_occurs=1), _returns=Iterable(Unicode, min_occurs=1))
def greets(ctx, part_of_days):
for part_of_day in part_of_days:
yield 'Good %s' % part_of_day
@rpc(Unicode, _returns=Unicode(min_occurs=0))
def say_maybe_nothing(ctx, name):
return
@rpc(Unicode, _returns=Unicode(min_occurs=0))
def say_maybe_something(ctx, name):
return 'Hello, %s' % name
@rpc()
def fault(ctx):
raise Fault(faultstring="a fault, as promised")
@rpc()
def secret(ctx):
secret = 'QWxhZGRpbjpPcGVuU2VzYW1l'
soap_token = next((h for h in (ctx.in_header_doc or []) if h.tag == '{http://tempuri.org/}Token'), None)
if soap_token is not None and soap_token.text == secret:
return
http_token = ctx.transport.req.get('HTTP_AUTHORIZATION', None)
if http_token == 'Basic ' + secret:
return
raise InvalidCredentialsError()
application = Application([HelloWorldService],
name='HelloWorld',
tns='spyne.examples.hello',
in_protocol=Soap11(validator='lxml'),
out_protocol=Soap11()
)
if __name__ == '__main__':
# You can use any Wsgi server. Here, we chose
# Python's built-in wsgi server but you're not
# supposed to use it in production.
from wsgiref.simple_server import make_server
wsgi_app = WsgiApplication(application)
server = make_server('0.0.0.0', 8000, wsgi_app)
server.serve_forever()