Serving binary content (images, etc) using BasteHTTPServer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MarkCSU@gmail.com

    Serving binary content (images, etc) using BasteHTTPServer

    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","r b") # file is harcoded until
    i get images being served correctly
    image = fileObj.read()
    s.wfile.write(i mage)
  • Diez B. Roggisch

    #2
    Re: Serving binary content (images, etc) using BasteHTTPServer

    MarkCSU@gmail.c om wrote:
    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","r b") # file is harcoded until
    i get images being served correctly
    image = fileObj.read()
    s.wfile.write(i mage)
    Don't you miss a Content-Length header?

    Diez

    Comment

    • Richard Brodie

      #3
      Re: Serving binary content (images, etc) using BasteHTTPServer


      <MarkCSU@gmail. comwrote in message news:556871d3-1fea-40f2-9cc6-
      s.end_headers
      A bare method name (without parentheses) won't get called.


      Comment

      Working...