BaseHttpServer

Steve Holden steve at holdenweb.com
Sun Feb 15 15:46:01 EST 2009


Paul wrote:
> Hi,
> I currently have a webserver using BaseHttpServe that serves images
> like this:
> if self.path.endswith(".jpg"):
>                 print(curdir + sep + self.path)
>                 f = open(curdir + sep + self.path,"b")
>                 self.send_response(200)
>                 self.send_header('Content-type',	'image/jpg')
>                 self.end_headers()
>                 self.wfile.write(f.read())
>                 f.close()
>                 return
> Whilst it works, it does take quite a while to load (approx 10secs for
> a 4mb file even though its over the local connection) - does anyone
> have any hints/tips for speeding it up?

You could consider reading the file in smaller blocks and writing the
output in a loop. That way the next block of the file can be read in
while the network buffers are emptying.

Just keep reading data and writing it until the number of data bytes you
wrote is fewer than the number you tried to read.

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list