Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > improving performance of python webserver running python scripts incgi-bin

Reply
Thread Tools

improving performance of python webserver running python scripts incgi-bin

 
 
Dale
Guest
Posts: n/a
 
      01-11-2008
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


 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      01-11-2008
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 ?
 
Reply With Quote
 
 
 
 
Mike Meyer
Guest
Posts: n/a
 
      01-11-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run CGI scripts in own webserver Kevin Kevin Ruby 7 03-22-2011 09:03 AM
Re: improving python performance by extension module (64bit) geremy condra Python 2 06-26-2010 11:55 AM
improving python performance by extension module (64bit) Peng Yu Python 0 06-25-2010 02:52 AM
CGIHTTPServer webserver running php scripts mpc Python 1 09-15-2008 11:28 PM
Using Python Scripts with IIS - ASP or Python-based CGI scripts withIIS - which makes more sense? davidj411 Python 0 06-27-2008 04:38 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57