11

I would like some help on my following handler:

 class MyHandler(http.server.BaseHTTPRequestHandler):
     def do_HEAD(client):
        client.send_response(200)
        client.send_header("Content-type", "text/html")
        client.end_headers()
     def do_GET(client):
        if client.path == "/":
           client.send_response(200)
           client.send_header("Content-type", "text/html")
           client.end_headers()

           client.wfile.write(load('index.html'))

 def load(file):
    with open(file, 'r') as file:
    return encode(str(file.read()))

 def encode(file):
    return bytes(file, 'UTF-8')

I've got this, the function load() is someone else in the file. Sending a HTML page over my HTTP handler seems to be working, but how can I send an image? How do I need to encode it and what Content-type should I use?

Help is greatly appreciated!

(PS: I would like the image that is send to be seen in the browser if I connect to my httpserver)

2 Answers 2

14

For a PNG image you have to set the content-type to "image/png". For jpg: "image/jpeg".

Other Content types can be found here.

Edit: Yes, I forgot about encoding in my first edit.

The answer is: You don't! When you load your image from a file, it is in the correct encoding already.

I read about your codec problem: The problem is, as much I see in your load function. Don't try to encode the file content.

You may use for binary data this:

def load_binary(filename):
    with open(filename, 'rb') as file_handle:
        return file_handle.read()
Sign up to request clarification or add additional context in comments.

5 Comments

When I change 'r' to 'rb' it loads for 3 minutes or so and I have 1000 lines of random words...
I just tried your load_binary but then I get this error: 'charmap' codec can't decode byte 0x81 in position 600: character maps to <undefined>
In this case, the method client.wfile.write does expect ascii data. I don't know the webserver you use -- maybe there is a different method for binary data?
it might be because im encoding it with 'UTF-8'.. should I encode it with something else?
No. Problem here is, that you are working with Python 3, as it seems. I have not so much experience with it yet. But the trouble with Python (2 & 3) is the encoding stuff. You must read the file as binary file, else Python will try to read it as text and that can not work with binary files. I changed my code above. Could you try this? ('rb' instead of 'r'). I hope, that the http module then will send the unencoded binary data.
3

As mentioned by Juergen you have to set the accordingly content-type. This example I found may help you: https://github.com/tanzilli/playground/blob/master/python/httpserver/example2.py

The example is in Python 2, but the changes should be minor.

Ah and it's better to use self instead of client -> see PEP 8, Python's style guide

2 Comments

this was kind of useful, but it's NOT easy to transition from python 2 to 3 because in python 3 you have to encode all the data into bytes... otherwise it won't work, I just tested.
I should have given more information... Line 39 has to be changed. You have to open it in binary mode: f = open(curdir + sep + self.path, 'rb') Moreover the print statements and the import (from http.server import [...] instead of from BaseHTTPServer import [...]) must be changed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.