Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7856769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:48:47+00:00 2026-06-02T20:48:47+00:00

I’m trying to write a simple Echo client in Twisted that sends keyboard input

  • 0

I’m trying to write a simple Echo client in Twisted that sends keyboard input to the server, and is terminated by the user entering ‘q’ on it’s own. In short, I’m just trying to modify the simple echo client (and variants) found on this page. Nothing sexy at all, just the basics.

I’m struggling with the very basic event loop. It looks like I can’t start/stop the reactor within the loop as a stopped reactor cannot be restarted. If I don’t stop the reactor, then I’ll never get to the next line that gets keyboard input.

Any help in getting my echo client working would be much appreciated.

from twisted.internet.protocol import ClientFactory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor

class EchoClient(LineReceiver):
    end="Bye-bye!"
    def connectionMade(self):
        #only write and end transmission if the message isn't empty
        if len(self.factory.message) > 0:
            self.sendLine(self.factory.message)
            self.sendLine(self.end)
        else:
        #Else just terminate the connection
            self.transport.loseConnection()

    def lineReceived(self, line):
        print "receive:", line
        if line==self.end:
            self.transport.loseConnection()

class EchoClientFactory(ClientFactory):
    message = ""

    def buildProtocol(self, address):
        p = EchoClient()
        p.factory = self
        return p

    def clientConnectionFailed(self, connector, reason):
        reactor.stop()

    def clientConnectionLost(self, connector, reason):
        reactor.stop()

def main():

    s = raw_input('Text to send (''q'' to terminate): ')
    while s != 'q':
        factory = EchoClientFactory()
        factory.message = s
        reactor.connectTCP('localhost', 8000, factory)

        #This is bad because reactor cannot be restarted once it's been stopped
        reactor.run()

        s = raw_input('Text to send(''q'' to terminate): ')

if __name__ == '__main__':
    main()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:48:49+00:00Added an answer on June 2, 2026 at 8:48 pm

    As a rule of a thumb – there are very rare instances where you would want to restart or stop reactor, unless you are terminating your program alltogeather. If your encounter a piece of code which will cause block e.g. database access, long computation or in your case raw_input, you have to either: find a twisted alternative (twisted.enterprise.adabi in case of database) or make it twisted compatible.
    The easiest way to ‘deblock’ your code is move the blocking bits into thread by utilizing deferToThread from twisted.internet.threads.
    Consider this example:

    from twisted.internet.threads import deferToThread as __deferToThread
    from twisted.internet import reactor
    
    def mmprint(s):
        print(s)
    
    class TwistedRAWInput(object):
        def start(self,callable,terminator):
            self.callable=callable
            self.terminator=terminator
            self.startReceiving()
        def startReceiving(self,s=''):
            if s!=self.terminator:
                self.callable(s)
                __deferToThread(raw_input,':').addCallback(self.startReceiving)
    
    
    tri = TwistedRAWInput()
    reactor.callWhenRunning(tri.start,mmprint,'q')
    reactor.run()
    

    You would never have to stop reactor, as raw_input would happen in an outside thread, callbacking deferred on every new line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.