Serving binary content (images, etc) using BasteHTTPServer

M

MarkCSU

I'm writing a simple web server in python using the BaseHTTPServer
library. I can serve text content (ie html pages) with ease, but im
running into troubles when i try to serve images. The image gets
corrupted in transit and when I manually download the image from the
website and look at it using a hex editor it appears that the first 60
(or first 3C in hex if it makes a helps) characters are missing.


My code looks like this:

def do_GET(s):

# code that determines that yes this is an image

s.send_response(200)
s.send_header("Content-type", "image/jpeg")
s.end_headers

fileObj = open("1.jpg","rb") # file is harcoded until
i get images being served correctly
image = fileObj.read()
s.wfile.write(image)
 
D

Diez B. Roggisch

I'm writing a simple web server in python using the BaseHTTPServer
library. I can serve text content (ie html pages) with ease, but im
running into troubles when i try to serve images. The image gets
corrupted in transit and when I manually download the image from the
website and look at it using a hex editor it appears that the first 60
(or first 3C in hex if it makes a helps) characters are missing.


My code looks like this:

def do_GET(s):

# code that determines that yes this is an image

s.send_response(200)
s.send_header("Content-type", "image/jpeg")
s.end_headers

fileObj = open("1.jpg","rb") # file is harcoded until
i get images being served correctly
image = fileObj.read()
s.wfile.write(image)

Don't you miss a Content-Length header?

Diez
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top