Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / web / examples / advogato.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 This example demonstrates how to logon to a remote server and post a diary.
6
7 Usage:
8     $ python advogato.py <name> <diary entry file>
9 """
10
11 import sys
12 from getpass import getpass
13
14 from twisted.web.xmlrpc import Proxy
15 from twisted.internet import reactor
16
17 class AddDiary:
18
19     def __init__(self, name, password):
20         self.name = name
21         self.password = password
22         self.proxy = Proxy('http://advogato.org/XMLRPC')
23
24     def __call__(self, filename):
25         self.data = open(filename).read()
26         d = self.proxy.callRemote('authenticate', self.name, self.password)
27         d.addCallbacks(self.login, self.noLogin)
28
29     def noLogin(self, reason):
30         print "could not login"
31         reactor.stop()
32
33     def login(self, cookie):
34         d = self.proxy.callRemote('diary.set', cookie, -1, self.data)
35         d.addCallbacks(self.setDiary, self.errorSetDiary)
36
37     def setDiary(self, response):
38         reactor.stop()
39
40     def errorSetDiary(self, error):
41         print "could not set diary", error
42         reactor.stop()
43
44 diary = AddDiary(sys.argv[1], getpass())
45 diary(sys.argv[2])
46 reactor.run()