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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:56:27+00:00 2026-06-17T18:56:27+00:00

I’m using python socket server to which I connect with Android and periodically send

  • 0

I’m using python socket server to which I connect with Android and periodically send messages.
I have a problem that the request is closed on every sent message and i need it to remain opened until Android decides to close it.

Curentlly it looks like this:

class SingleTCPHandler(SocketServer.StreamRequestHandler):
    def handle(self):
    try:
        while True:
            message = self.rfile.readline().strip()  # clip input at 1Kb            
            my_event = pygame.event.Event(USEREVENT, {'control':message})
            pygame.event.post(my_event)
    except KeyboardInterrupt:
        sys.exit(0)
    finally:
        self.request.close()

I’ve solved this by adding a while True in my handle() definition, however, this was criticized as a bad solution and that the right way to go is to override the process_request and shutdown methods.

Attempt of solution

I removed the while from the code, connected to the server locally with netcat, sent a message and went to see when will the connection be closed.

I wanted to see what is the method after which the connection is being closed to figuer out what i have to override.

I have stepped with the debugger through the serve_forever() and followed it to this part of code:

> /usr/lib/python2.7/threading.py(495)start()
    494         try:
--> 495             _start_new_thread(self.__bootstrap, ())
    496         except Exception:

After line 495 is passed (i can’t step into it) the connection is closed.

I somehow doubt that it’s such a hustle to maintain a connection via socket, that is basically the reason why we chosen to communicate over a socket, to have a continuous connection and not a ‘one connection per sent message’ system.

Ideas on implementation, or links?

  • 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-17T18:56:29+00:00Added an answer on June 17, 2026 at 6:56 pm

    The handle method is called for each client connection, and the connection is closed when it returns. Using a while loop is fine. Exit the loop when the client closes the connection.

    Example (Python 3 syntax):

    class EchoHandler(socketserver.StreamRequestHandler):
    
        def setup(self):
            print('{}:{} connected'.format(*self.client_address))
    
        def handle(self):
            while True:
                data = self.request.recv(1024)
                if not data: break
                self.request.sendall(data)
    
        def finish(self):
            print('{}:{} disconnected'.format(*self.client_address))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I have been unable to fix a problem with Java Unicode and encoding. The
I have thousands of HTML files to process using Groovy/Java and I need to
I have a view passing on information from a database: def serve_article(request, id): served_article

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.