Advertisement

02.05.2007 at 06:02PM PST, ID: 22150686
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.2

Simple Client

Asked by templeavenue in Python Scripting Language

Tags: ,

Hi,
 I am trying to write a simple client program.  It will connect to other "terminal/server" with ip address known ahead of time at some port (8889).  Then it will listen to the port connected.  I have written this code, but it will not work (crash). Basically, I have written the code to connect to other terminal first.  Once it is connected, then I call "listenTCP" to listen to the port until it get "quit". (This is a modify version of the twisted code I have found from other sources, but I think it should work).  The program would terminate by itself after a few seconds (3-4s). Can someone tell me what is wrong with the code? Thanks in advance.


from twisted.internet import reactor, defer, protocol
from twisted.protocols import basic


class CallbackAndDisconnectProtocol(protocol.Protocol):
    def connectionMade(self):
        self.factory.deferred.callback("Connected!")
       
class ConnectionTestFactory(protocol.ClientFactory):
    protocol = CallbackAndDisconnectProtocol

    def __init__(self):
        self.deferred = defer.Deferred( )

    def clientConnectionFailed(self, connector, reason):
        self.deferred.errback(reason)

class EchoProtocol(basic.LineReceiver):
    def lineReceived(self, line):
        if line == 'quit':
            self.sendLine("Goodbye.")
            self.transport.loseConnection( )
        else:
            self.sendLine("You said: " + line)
            reactor.stop()
           
       
class EchoServerFactory(protocol.ServerFactory):
    print "listening..."
    protocol  = EchoProtocol

def testConnect(host, port):
    testFactory = ConnectionTestFactory( )
    reactor.connectTCP(host, port, testFactory)
    return testFactory.deferred

def handleSuccess(result, port):
    print "Connected to port %i" % port    
    reactor.listenTCP(port,EchoServerFactory())        

def handleFailure(failure, port):
    print "Error connecting to port %i: %s" % (
        port, failure.getErrorMessage( ))
    reactor.stop( )
   
if __name__ == "__main__":
    import sys    
   
    host = '192.168.0.2'    
    port = 8889
    print "Connecting to ", host, " at port " , port
    connecting = testConnect(host, port)
    connecting.addCallback(handleSuccess, port)
    connecting.addErrback(handleFailure, port)
    reactor.run( )
Start Free Trial
[+][-]02.18.2007 at 10:29PM PST, ID: 18561308

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.27.2007 at 12:51AM PST, ID: 18615478

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Python Scripting Language
Tags: client, simple
Sign Up Now!
Solution Provided By: pepr
Participating Experts: 3
Solution Grade: B
 
 
[+][-]05.09.2007 at 09:43AM PDT, ID: 19058402

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32