Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / core / howto / listings / amp / command_client.py
1
2 if __name__ == '__main__':
3     import command_client
4     raise SystemExit(command_client.main())
5
6 from sys import stdout
7
8 from twisted.python.log import startLogging, err
9 from twisted.protocols.amp import Integer, String, Unicode, Command
10 from twisted.internet import reactor
11
12 from basic_client import connect
13
14 class UsernameUnavailable(Exception):
15     pass
16
17
18 class RegisterUser(Command):
19     arguments = [('username', Unicode()),
20                  ('publickey', String())]
21
22     response = [('uid', Integer())]
23
24     errors = {UsernameUnavailable: 'username-unavailable'}
25
26
27 def main():
28     startLogging(stdout)
29
30     d = connect()
31     def connected(protocol):
32         return protocol.callRemote(
33             RegisterUser,
34             username=u'alice',
35             publickey='ssh-rsa AAAAB3NzaC1yc2 alice@actinium')
36     d.addCallback(connected)
37
38     def registered(result):
39         print 'Registration result:', result
40     d.addCallback(registered)
41
42     d.addErrback(err, "Failed to register")
43
44     def finished(ignored):
45         reactor.stop()
46     d.addCallback(finished)
47
48     reactor.run()