Skip to content

Instantly share code, notes, and snippets.

@Drvanon
Created May 2, 2012 12:25
Show Gist options
  • Save Drvanon/2576212 to your computer and use it in GitHub Desktop.
Save Drvanon/2576212 to your computer and use it in GitHub Desktop.
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'>>)]
# 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))
#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