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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:14:42+00:00 2026-05-27T08:14:42+00:00

I am using python asynchat to implement a network protocol. At connection time I

  • 0

I am using python asynchat to implement a network protocol.
At connection time I need to send a command and the server answer with a session.

My main problem is that I need to wait until I get the session response. but not sure how to implement this. should I use socket.recv for the connection setup? is a good idea?

  • 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-05-27T08:14:42+00:00Added an answer on May 27, 2026 at 8:14 am

    When writing a network application using asynchronous techniques, you wait by recording your state somewhere and then letting the main loop continue. At some future time, the data you’re waiting for will become available, the main loop will notify you of that fact, and you can combine the new data with the recorded state to complete whatever task you are working on. Depending on the specific task, you may need to go through this cycle many times before your task is actually done.

    These ideas are basically the same regardless of what asynchronous system you’re using. However, Twisted is a vastly superior system to asynchat, so I’m not going to try to explain any of the asynchat details. Instead, here’s an example that does the kind of thing you’re asking about, using Twisted:

    from twisted.internet.defer import Deferred
    from twisted.internet.protocol import Protocol, Factory
    from twisted.internet.endpoints import TCP4ClientEndpoint
    from twisted.internet import reactor
    
    # Stream-oriented connections like TCP are handled by an instance 
    # of a Protocol subclass
    class SomeKindOfClient(Protocol):
    
         # When a new connection is established, the first thing that
         # happens is this method is called.
         def connectionMade(self):
             # self.transport is set by the superclass, and lets us 
             # send data over the connection
             self.transport.write("GREETING")
    
             # a Deferred is a generic, composable API for specifying
             # callbacks
             self.greetingComplete = Deferred()
    
             # Here's some local state
             self._buffer = ""
    
         # Whenever bytes arrive on the TCP connection, they're passed
         # to this method
         def dataReceived(self, bytes):
    
             # Incorportate the network event data into our local state.
             #  This kind of buffering is always necessary with TCP, because
             # there's no guarantees about how many bytes will be delivered
             # at once (except that it will be at least 1), regardless of
             # the size of the send() the peer did.
             self._buffer += bytes
    
             # Figure out if we're done - let's say the server response is 32
             # bytes of something
             if len(self._buffer) >= 32:
                 # Deliver it to whomever is waiting, by way of the Deferred
                 # object
                 greeting, self._buffer = self._buffer[:32], self._buffer[32:]
                 complete = self.greetingComplete
                 self.greetingComplete = None
                 complete.callback(greeting)
    
             # Otherwise we'll keep waiting until dataReceived is called again
             # and we have enough bytes.
    
    
    # One of the normal ways to create a new client connection
    f = Factory()
    f.protocol = SomeKindOfClient
    e = TCP4ClientEndpoint(reactor, "somehost", 1234)
    
    # Connect returns one of those Deferreds - letting us specify a function
    # to call when the connection is established.  The implementation of
    # connect is also doing basically the same kind of thing as you're asking
    # about.
    d = e.connect(f)
    
    # Execution continues to this point before the connection has been
    # established.  Define a function to use as a callback when the connection
    # does get established.
    def connected(proto):
        # proto is an instance of SomeKindOfClient.  It has the 
        # greetingComplete attribute, which we'll attach a callback to so we
        # can "wait" for the greeting to be complete.
        d = proto.greetingComplete
    
        def gotGreeting(greeting):
            # Note that this is really the core of the answer.  This function
            # is called *only* once the protocol has decided it has received
            # some necessary data from the server.  If you were waiting for a
            # session identifier of some sort, this is where you might get it
            # and be able to proceed with the remainder of your application
            # logic.
            print "Greeting arrived", repr(greeting)
    
        # addCallback is how you hook a callback up to a Deferred - now
        # gotGreeting will be called when d "fires" - ie, when its callback
        # method is invoked by the dataReceived implementation above.
        d.addCallback(gotGreeting)
    
    # And do the same kind of thing to the Deferred we got from 
    # TCP4ClientEndpoint.connect
    d.addCallback(connected)
    
    # Start the main loop so network events can be processed
    reactor.run()
    

    To see how this behaves, you can launch a simple server (eg nc -l 1234) and point the client at it. You’ll see the greeting arrive and you can send some bytes back. Once you’ve sent back 30, the client will print them (and then hang around indefinitely, because I implemented no further logic in that protocol).

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

Sidebar

Related Questions

Using Python's Networkx library, I created an undirected graph to represent a relationship network
I am using python for making a COM local server. In fact, the whole
Using Python, I'm storing a date & time as datetime.datetime into GAE. Is there
I'm using asynchat and trying to use python3. getting this error: error: uncaptured python
Using python I have to retrieve value of time and date of an event
Using Python I can test my code in the terminal / command line by
Using Python I want to be able to draw text at different angles using
Using python 2.4 and the built-in ZipFile library, I cannot read very large zip
Using Python, how would I go about reading in (be from a string, file
Using Python 2.6, is there a way to check if all the items of

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.