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 7919737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:57:49+00:00 2026-06-03T15:57:49+00:00

I have been asked to write a class that connects to a server, asynchronously

  • 0

I have been asked to write a class that connects to a server, asynchronously sends the server various commands, and then provides the returned data to the client. I’ve been asked to do this in Python, which is a new language to me. I started digging around and found the Twisted framework which offers some very nice abstractions (Protocol, ProtocolFactory, Reactor) that do a lot of the things that I would have to do if I would roll my own socket-based app. It seems like the right choice given the problem that I have to solve.

I’ve looked through numerous examples on the web (mostly Krondo), but I still haven’t seen a good example of creating a client that will send multiple commands across the wire and I maintain the connection I create. The server (of which I have no control over), in this case, doesn’t disconnect after it sends the response. So, what’s the proper way to design the client so that I can tickle the server in various ways?

Right now I do this:

class TestProtocol(Protocol)
    def connectionMade(self):
         self.transport.write(self.factory.message)

class TestProtocolFactory(Factory):
    message = ''
    def setMessage(self, msg):
        self.message = msg

def main():
    f = TestProtocolFactory()
    f.setMessage("my message")
    reactor.connectTCP(...)
    reactor.run()

What I really want to do is call self.transport.write(...) via the reactor (really, call TestProtocolFactory::setMessage() on-demand from another thread of execution), not just when the connection is made.

  • 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-03T15:57:50+00:00Added an answer on June 3, 2026 at 3:57 pm

    Depends. Here are some possibilities:

    I’m assuming

    Approach 1. You have a list of commands to send the server, and for some reason can’t do them all at once. In that case send a new one as the previous answer returns:

    class proto(parentProtocol):
        def stringReceived(self, data):
            self.handle_server_response(data)
            next_command = self.command_queue.pop()
            # do stuff
    

    Approach 2. What you send to the server is based on what the server sends you:

    class proto(parentProtocol):
        def stringReceived(self, data):
            if data == "this":
                self.sendString("that")
            elif data == "foo":
                self.sendString("bar")
            # and so on
    

    Approach 3. You don’t care what the server sends to, you just want to periodically send some commands:

    class proto(parentProtocol):
        def callback(self):
            next_command = self.command_queue.pop()
            # do stuff
        def connectionMade(self):
            from twisted.internet import task
            self.task_id = task.LoopingCall(self.callback)
            self.task_id.start(1.0)
    

    Approach 4: Your edit now mentions triggering from another thread. Feel free to check the twisted documentation to find out if proto.sendString is threadsafe. You may be able to call it directly, but I don’t know. Approach 3 is threadsafe though. Just fill the queue (which is threadsafe) from another thread.

    Basically you can store any amount of state in your protocol; it will stay around until you are done. The you either send commands to the server as a response to it’s messages to you, or you set up some scheduling to do your stuff. Or both.

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

Sidebar

Related Questions

I have been asked to write an application that is going to act as
I have been asked to write a GUI that will allow objects to be
I have been asked to write a SQL script that can be run, which
Background I have been asked to write a script that will create a vertical
I have been asked to write a java program on linux platform. According to
I have been asked to write a program using python for an assignment. I
I have been asked to write a compliment website/service for an iPhone app. The
I have been asked in an interview to write a SQL query which fetches
I have been asked to lend a hand on a hobby project that a
I have been asked to develop some usercontrols in ASP.NET that will at a

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.