Created
May 2, 2012 12:25
-
-
Save Drvanon/2576212 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Traceback (most recent call last): | |
Failure: twisted.internet.error.ConnectionRefusedError: Connection was refused by other side: 111: Connection refused. | |
Done with math: [(True, 94), (False, <twisted.python.failure.Failure <class 'twisted.internet.error.ConnectionRefusedError'>>)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Read username, output from non-empty factory, drop connections | |
# Use deferreds, to minimize synchronicity assumptions | |
# Write application. Save in 'finger.tpy' | |
from twisted.application import internet, service | |
from twisted.internet import protocol, reactor, defer | |
from twisted.protocols import basic | |
class FingerProtocol(basic.LineReceiver): | |
def lineReceived(self, user): | |
if 'add'in user: | |
print user | |
a = user.split(' ', 2) | |
print a | |
a.pop(0) | |
print a | |
factory.users[a[0]] = a[1] | |
print factory.users | |
else: | |
d = self.factory.getUser(user) | |
def onError(err): | |
return 'Internal error in server' | |
if d: | |
d.addErrback(onError) | |
def writeResponse(message): | |
self.transport.write(message + '\r\n') | |
if d: | |
d.addCallback(writeResponse) | |
class FingerFactory(protocol.ServerFactory): | |
protocol = FingerProtocol | |
def __init__(self, users): | |
self.users = users | |
def getUser(self, user): | |
return defer.succeed(self.users.get(user, "No such user")) | |
users = {'moshez':'happy and well', 'Rob':'Programming','Danny':'being happy'} | |
application = service.Application('finger', uid=1, gid=1) | |
factory = FingerFactory(users) | |
internet.TCPServer(1079, factory).setServiceParent( | |
service.IServiceCollection(application)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://twistedmatrix.com/documents/current/api/twisted.protocols.amp.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#server | |
sudo twistd -ny first.tac | |
#client | |
add Eliza playing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment