3
# Copyright (c) 2009 Twisted Matrix Laboratories.
4
# See LICENSE for details.
7
Simple PB Words client demo
9
This connects to a server (host/port specified by argv[1]/argv[2]),
10
authenticates with a username and password (given by argv[3] and argv[4]),
11
joins a group (argv[5]) sends a simple message, leaves the group, and quits
16
from twisted.python import log
17
from twisted.cred import credentials
18
from twisted.words import service
19
from twisted.spread import pb
20
from twisted.internet import reactor
22
class DemoMind(service.PBMind):
23
"""An utterly pointless PBMind subclass.
25
This notices messages received and prints them to stdout. Since
26
the bot never stays in a channel very long, it is exceedingly
27
unlikely this will ever do anything interesting.
29
def remote_receive(self, sender, recipient, message):
30
print 'Woop', sender, recipient, message
32
def quitServer(ignored):
33
"""Quit succeeded, shut down the reactor.
37
def leftGroup(ignored, avatar):
38
"""Left the group successfully, quit the server.
41
q.addCallback(quitServer)
44
def sentMessage(ignored, group, avatar):
45
"""Sent the message successfully, leave the group.
48
l.addCallback(leftGroup, avatar)
51
def joinedGroup(group, avatar):
52
"""Joined the group successfully, send a stupid message.
54
s = group.send({"text": "Hello, monkeys"})
55
s.addCallback(sentMessage, group, avatar)
58
def loggedIn(avatar, group):
59
"""Logged in successfully, join a group.
61
j = avatar.join(group)
62
j.addCallback(joinedGroup, avatar)
65
def errorOccurred(err):
66
"""Something went awry, log it and shutdown.
74
def run(host, port, username, password, group):
75
"""Create a mind and factory and set things in motion.
78
f = pb.PBClientFactory()
79
f.unsafeTracebacks = True
80
l = f.login(credentials.UsernamePassword(username, password), m)
81
l.addCallback(loggedIn, group)
82
l.addErrback(errorOccurred)
83
reactor.connectTCP(host, int(port), f)
87
Set up logging, have the real main function run, and start the reactor.
89
if len(sys.argv) != 6:
90
raise SystemExit("Usage: %s host port username password group" % (sys.argv[0],))
91
log.startLogging(sys.stdout)
93
host, port, username, password, group = sys.argv[1:]
95
username = username.decode(sys.stdin.encoding)
96
group = group.decode(sys.stdin.encoding)
98
reactor.callWhenRunning(run, host, port, username, password, group)
101
if __name__ == '__main__':