examples/twisted/echoserver.py
from twisted.internet import protocol,reactor

class Echo(protocol.Protocol):
  def dataReceived(self, data):
    self.transport.write(data)

class EchoFactory(protocol.Factory):
  def buildProtocol(self, addr):
    return Echo()

reactor.listenTCP(8000, EchoFactory())
reactor.run()
examples/twisted/echoclient.py
from twisted.internet import reactor,protocol

class EchoClient(protocol.Protocol):
  def connectionMade(self):
    self.transport.write("Hello")

  def dataReceived(self, data):
    print "Server said:", data
    self.transport.loseConnection()

class EchoFactory(protocol.ClientFactory):
  def buildProtocol(self, addr):
    return EchoClient()

  def clientConnectionFailed(self, connector, reason):
    print "connection failed"
    reactor.stop()

  def clientConnectionLost(self, connector, reason):
     print "connection lost"
     reactor.stop()

reactor.connectTCP("localhost", 8000, EchoFactory())
reactor.run()