Write image to webpage with python 3
Question: I have currently got this code set up:

    
    
        import time
        import http.server
        import socketserver
        import mimetypes
        import os
    
        HOST_NAME = 'localhost'
        PORT = 8000
    
    
        def load(self):
            with open(self, 'r') as self:
            self = self.read()
            return self.encode('UTF-8')
    
    
    
        class MyHandler(http.server.BaseHTTPRequestHandler):
            def do_HEAD(self):
                self.send_response(200)
                self.send_header("Content-type", "text/html")
                self.end_headers()
                def do_GET(self):
                        if self.path == "/":
                            self.send_response(200)
                            self.send_header("Content-type", "text/html")
                            self.end_headers()
                            self.wfile.write(load('index.html'))
                        if self.path.endswith('.css'):
                            self.send_response(200)
                            self.send_header("Content-type", "text/css")
                            self.end_headers()
                            dest = self.path.replace("/", "")
                            self.wfile.write(load(dest))
                        if self.path == "/bg.jpg":
                            self.send_response(200)
                            self.send_header("Content-type", "image/jpeg")
                            self.end_headers()
    
        if __name__ == '__main__':
            httpd = socketserver.TCPServer(("", PORT), MyHandler)
            print(time.asctime(), "Server Starts - %s:%s" % ("", PORT))
            try:
                httpd.serve_forever()
            except KeyboardInterrupt:
                pass
            httpd.server_close()
            print(time.asctime(), "Server Stops - %s:%s" % ("", PORT))
    

My webpage seems to be working and I get my index.html and css loaded when I
open up my webpage, however the image is not coming through, has anyone got
any idea why?


Answer: To send an image over HTTP, just write the image data directly to the socket,
like for any other type of file. You can't use your `load()` function for
this, since that encodes the text as UTF-8. Instead, you should open the file
with mode `rb` or similar, read a `bytes` from that filehandle, then write it
directly to `self.wfile` in the HTTP handler.

