3
from twisted.application import service, internet
4
from twisted.cred.checkers import FilePasswordDB
5
from twisted.web2 import server
6
from twisted.web2.channel import http
8
from txjsonrpc.auth import wrapResource
9
from txjsonrpc.web2 import jsonrpc
12
class Example(jsonrpc.JSONRPC):
13
"""An example object to be published."""
17
def jsonrpc_echo(self, request, x):
18
"""Return all passed args."""
21
def jsonrpc_add(self, request, a, b):
22
"""Return sum of arguments."""
26
# Set up the application and the JSON-RPC resource.
27
application = service.Application("Example JSON-RPC Server")
30
# Define the credential checker the application will be using and wrap the
32
dirname = os.path.dirname(__file__)
33
checker = FilePasswordDB(os.path.join(dirname, "passwd.db"))
34
realmName = "My JSON-RPC App"
35
wrappedRoot = wrapResource(root, [checker], realmName=realmName)
37
# With the wrapped root, we can set up the server as usual.
38
site = server.Site(wrappedRoot)
39
chan = http.HTTPFactory(site)
40
jsonrpcServer = internet.TCPServer(7080, chan)
41
jsonrpcServer.setServiceParent(application)