BaseHTTPServer ThreadMixIn not working

Discussion in 'Python' started by amit, Oct 3, 2011.

  1. amit

    amit Guest

    Hi everyone,
    I am really stuck in a very simple problem and wanted to know if you
    could take some time to check it out -

    My code is simple. Using BaseHTTPServer and ThreadInMix I want to run
    a python script (Script1.py) for every request made simultaneously.

    My code->

    from subprocess import PIPE, Popen
    from SocketServer import ThreadingMixIn
    from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
    import time

    def simple_script(self):
    print 'simple_script'
    s = Popen('C:/Python27/python C:/Script1.py 5', shell=True,
    stdout=PIPE, stderr=PIPE)
    out, err = s.communicate()
    print out, err
    self.wfile.write(out)

    class Handler(BaseHTTPRequestHandler):
    def do_GET(self):
    self.send_response(200)
    self.end_headers()
    self.wfile.write('{0}\n'.format(time.asctime()))
    simple_script(self)
    return

    class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
    pass

    if __name__ == '__main__':
    server = ThreadedHTTPServer(('', 8080), Handler)
    print 'Starting server, use <Ctrl-C> to stop'
    server.serve_forever()

    """
    # C:/Script1.py
    import time, sys

    s = time.time()

    while True:
    if time.time() - s > int(sys.argv[1]):
    break
    else:
    time.sleep(1)
    print time.asctime()
    """

    If I open multiple tabs/pages in Chrome/Firefox/IE and give URL:
    http://localhost:8080, the pages wait for previous page? This does not
    imply threading? Any help? Thanks
    amit, Oct 3, 2011
    #1
    1. Advertising

  2. En Mon, 03 Oct 2011 12:03:18 -0300, amit <> escribió:

    > I am really stuck in a very simple problem and wanted to know if you
    > could take some time to check it out -
    >
    > My code is simple. Using BaseHTTPServer and ThreadInMix I want to run
    > a python script (Script1.py) for every request made simultaneously.
    > [...]
    >
    > If I open multiple tabs/pages in Chrome/Firefox/IE and give URL:
    > http://localhost:8080, the pages wait for previous page? This does not
    > imply threading? Any help? Thanks


    Your code is fine, and Python behaves correctly. The browser is queuing
    all similar requests when it sees they all refer to the same URI. Had the
    first response contained an Expires: header in the future, there would be
    no need to ask again for the same object; the ETag: and Last-Modified:
    headers may play a role too. So, only after the first response is
    completely read, Chrome/Firefox/IE sees it is invalid and knows she cannot
    re-use the received body and has to issue the second request and waits
    again and ...

    Try with different URLs for each request:
    http://localhost:8080/a
    http://localhost:8080/b
    http://localhost:8080/c
    and you'll see they all are processed in parallel.


    --
    Gabriel Genellina
    Gabriel Genellina, Oct 5, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Molly
    Replies:
    0
    Views:
    495
    Molly
    Aug 25, 2003
  2. Joshua W. Biagio
    Replies:
    0
    Views:
    579
    Joshua W. Biagio
    Sep 30, 2003
  3. Yin
    Replies:
    1
    Views:
    387
    Eddie Corns
    Jan 26, 2004
  4. David
    Replies:
    1
    Views:
    853
    Christos TZOTZIOY Georgiou
    Jul 28, 2004
  5. Adam Tauno Williams
    Replies:
    2
    Views:
    563
    Adam Tauno Williams
    Jan 17, 2010
Loading...

Share This Page