improving performance of python webserver running python scripts incgi-bin

Discussion in 'Python' started by Dale, Jan 11, 2008.

  1. Dale

    Dale Guest

    I am using a simple python webserver (see code below) to serve up
    python scripts located in my cgi-bin directory.

    import BaseHTTPServer
    import CGIHTTPServer
    class Handler(CGIHTTPServer.CGIHTTPRequestHandler):
    cgi_directories = ['/cgi-bin']
    httpd = BaseHTTPServer.HTTPServer(('',8000), Handler)
    httpd.serve_forever()


    This works fine, but now I would like to combine the python scripts
    into the server program to eliminate starting the python interpreter
    on each script call. I am new to python, and was wondering if there
    is a better techique that will be faster.

    Also, can someone reccommend an alternative approach to
    httpd.serve_forever(). I would like to perform other python functions
    (read a serial port, write to an Ethernet port, write to a file, etc.)
    inside the web server program above. Is there an example of how to
    modify the code for an event loop style of operation where the program
    mostly performs my python I/O functions until an HTTP request comes
    in, and then it breaks out of the I/O operations to handle the HTTP
    request.

    thanks
    Dale
    Dale, Jan 11, 2008
    #1
    1. Advertising

  2. Re: improving performance of python webserver running python scriptsin cgi-bin

    Dale a écrit :
    > I am using a simple python webserver (see code below) to serve up
    > python scripts located in my cgi-bin directory.
    >
    > import BaseHTTPServer
    > import CGIHTTPServer
    > class Handler(CGIHTTPServer.CGIHTTPRequestHandler):
    > cgi_directories = ['/cgi-bin']
    > httpd = BaseHTTPServer.HTTPServer(('',8000), Handler)
    > httpd.serve_forever()
    >
    >
    > This works fine, but now I would like to combine the python scripts
    > into the server program to eliminate starting the python interpreter
    > on each script call. I am new to python, and was wondering if there
    > is a better techique that will be faster.
    >
    > Also, can someone reccommend an alternative approach to
    > httpd.serve_forever(). I would like to perform other python functions
    > (read a serial port, write to an Ethernet port, write to a file, etc.)
    > inside the web server program above. Is there an example of how to
    > modify the code for an event loop style of operation where the program
    > mostly performs my python I/O functions until an HTTP request comes
    > in, and then it breaks out of the I/O operations to handle the HTTP
    > request.
    >

    May I suggest that you take a look at more sophisticated solutions, like
    either wsgi, CherryPy or Twisted ?
    Bruno Desthuilliers, Jan 11, 2008
    #2
    1. Advertising

  3. Dale

    Mike Meyer Guest

    Re: improving performance of python webserver running pythonscripts in cgi-bin

    On Thu, 10 Jan 2008 23:17:28 -0800 (PST) Dale <> wrote:

    > I am using a simple python webserver (see code below) to serve up
    > python scripts located in my cgi-bin directory.
    >
    > import BaseHTTPServer
    > import CGIHTTPServer
    > class Handler(CGIHTTPServer.CGIHTTPRequestHandler):
    > cgi_directories = ['/cgi-bin']
    > httpd = BaseHTTPServer.HTTPServer(('',8000), Handler)
    > httpd.serve_forever()
    >
    >
    > This works fine, but now I would like to combine the python scripts
    > into the server program to eliminate starting the python interpreter
    > on each script call. I am new to python, and was wondering if there
    > is a better techique that will be faster.


    You can use BaseHTTPRequestHandler and override do_GET to handle the
    actual request.

    > Also, can someone reccommend an alternative approach to
    > httpd.serve_forever(). I would like to perform other python functions
    > (read a serial port, write to an Ethernet port, write to a file, etc.)
    > inside the web server program above. Is there an example of how to
    > modify the code for an event loop style of operation where the program
    > mostly performs my python I/O functions until an HTTP request comes
    > in, and then it breaks out of the I/O operations to handle the HTTP
    > request.


    Use poll/select/whatever to check if httpd.socket has data available,
    and then invoke httpd.handle_request() when it does. Nothing else will
    happen while the request is being handled.

    The suggestion to check the various frameworks for doing async work is
    a good one as well.

    <mike
    --
    Mike Meyer <> http://www.mired.org/consulting.html
    Independent Network/Unix/Perforce consultant, email for more information.
    Mike Meyer, Jan 11, 2008
    #3
    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. John Salerno
    Replies:
    30
    Views:
    1,941
    Stephan Kuhagen
    Aug 10, 2006
  2. Yves Dorfsman

    #!/usr/bin/env python vs. #!/usr/bin/python

    Yves Dorfsman, May 2, 2008, in forum: Python
    Replies:
    27
    Views:
    1,974
    Tim Roberts
    May 10, 2008
  3. mpc
    Replies:
    1
    Views:
    2,455
    Michael Palmer
    Sep 16, 2008
  4. anne001
    Replies:
    1
    Views:
    402
  5. Gilles
    Replies:
    7
    Views:
    544
    Matej Cepl
    Sep 30, 2012
Loading...

Share This Page