1
import socketserver
from http.server import SimpleHTTPRequestHandler
from threading import Timer, Event
from urllib.request import urlopen

class Responder( SimpleHTTPRequestHandler ):
    evt = Event(  )

    def do_GET( self ):
        print( 'req:', self.path )

        if self.path == '/block':
            Responder.evt.wait(  )
            self.respond( self.path )

        elif self.path == '/unblock':
            Responder.evt.set(  )
            self.respond( self.path )

        else:
            pass
            # self.respond( self.path )

    def respond( self, response ):
        self.send_response( 200 )
        self.send_header( 'Content-type', 'text/plain' )
        self.end_headers(  )
        self.wfile.write( bytes( str( response ), 'utf8' ) )

# create and start server
httpd = socketserver.TCPServer( ( '', 80 ), Responder )
Timer( 0, lambda: httpd.serve_forever(  ) ).start(  )

# just request the stated URL
def req_async( url, sec_delay = 0 ):
    Timer( sec_delay, lambda: print( 'resp:', urlopen( url ).read(  ) ) ).start(  )

req_async( 'http://localhost/block', 1 )
req_async( 'http://localhost/unblock', 2 )

req_async( 'http://localhost/foo' ) # closed connection without response !! but ..
# .. why not keep response stream open till I decide to write something on it ..
# .. or close it ?!

print( 0 )
This program creates a local server on port 80,
and requests that server to
    (1) block after 1 second, and
    (2) unblock after 2 seconds.

The class Responder has one threading.Event object,
    Responder.evt,
which I want other requests to block/unblock on.

When I get request on /block path, I wait on evt.
When I get request on /unblock path, I release waiters.

But only request (1) gets honored, and (2) is nowhere
in the scenes :D
Is it because (1) has blocked the server thread ?!
Is it because evt is shared ?

Can someone tell what happens there and why ?! TY ! :)

And also, how to properly do this in case it does block
the server thread.

And also see the (3)rd request .. how to keep the
response stream open ?

So, all in all,
    [1] why python doesn't keep response stream open ? and
    [2] why request (2) is not getting logged ?

Thank you and regards,
Ankur
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Ankur
  • 149
  • 7

1 Answers1

0

First, I should use BaseHTTPRequestHandler instead of SimpleHTTPRequestHandler to implement the handler.

Second, to answer [2], both the above classes will handle only a single request at a time. So, request (2) in above program cannot be handled until (1) is handled, but since (1) is blocked, there is no way of (2) getting logged.

To make above handlers multi threaded, see this answer.

Third, answering to [1], I still am not sure but I guess it behaves like that:

if do_GET / do_POST returns without writing a response, then I guess the handler closes the response stream.

Ankur
  • 149
  • 7