| 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.internet import reactor |
| 15 |
from twisted.web.xmlrpc import Proxy |
| 16 |
|
| 17 |
|
| 18 |
class AddDiary: |
| 19 |
def __init__(self, name, password): |
| 20 |
self.name = name |
| 21 |
self.password = password |
| 22 |
self.proxy = Proxy(b"http://advogato.org/XMLRPC") |
| 23 |
|
| 24 |
def __call__(self, filename): |
| 25 |
with open(filename) as f: |
| 26 |
self.data = f.read() |
| 27 |
d = self.proxy.callRemote("authenticate", self.name, self.password) |
| 28 |
d.addCallbacks(self.login, self.noLogin) |
| 29 |
|
| 30 |
def noLogin(self, reason): |
| 31 |
print("could not login") |
| 32 |
reactor.stop() |
| 33 |
|
| 34 |
def login(self, cookie): |
| 35 |
d = self.proxy.callRemote("diary.set", cookie, -1, self.data) |
| 36 |
d.addCallbacks(self.setDiary, self.errorSetDiary) |
| 37 |
|
| 38 |
def setDiary(self, response): |
| 39 |
reactor.stop() |
| 40 |
|
| 41 |
def errorSetDiary(self, error): |
| 42 |
print("could not set diary", error) |
| 43 |
reactor.stop() |
| 44 |
|
| 45 |
|
| 46 |
diary = AddDiary(sys.argv[1], getpass()) |
| 47 |
diary(sys.argv[2]) |
| 48 |
reactor.run() |