BaseHttpServer

Pierre Quentel quentel.pierre at wanadoo.fr
Sun Feb 15 15:31:50 EST 2009


On 15 fév, 18:31, Paul <pauljeffer... at gmail.com> 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?
> Thanks,
> Paul

Hi,

This is probably because you first load the file content in memory by
f.read() before sending it. In this case it's better to send the
content by chunk, using shutil : replace

    self.wfile.write(f.read())
by :
    shutil.copyfileobj(f,self.wfile)

- Pierre



More information about the Python-list mailing list