I am trying to make a HTTP server in Python3. For the beginning, I only want a server that serves a single JPG file. Here's my code:
from wsgiref.simple_server import make_server
def simple_app(environ, start_response):
headers = [('Content-type', 'image/jpeg')]
start_response('200 OK', headers)
data = b''
filename = r'sunset-at-dusk.jpg'
with open(filename, 'rb', buffering=0) as f:
data = f.readall()
print(type(data)) #<class 'bytes'>
return data
httpd = make_server('', 8001, simple_app)
print("Serving on port 8001...")
httpd.serve_forever()
When I try to access the server via HTTP, I get the following error.
127.0.0.1 - - [01/Jun/2016 08:37:43] "GET / HTTP/1.1" 200 0 Traceback (most recent call last): File "C:\Anaconda3\lib\wsgiref\handlers.py", line 138, in run
self.finish_response() File "C:\Anaconda3\lib\wsgiref\handlers.py", line 180, in finish_response
self.write(data) File "C:\Anaconda3\lib\wsgiref\handlers.py", line 266, in write
"write() argument must be a bytes instance" AssertionError: write() argument must be a bytes instance
'sunset-at-dusk.jpg' is a valid JPG file in the same folder as my script.
What am I doing wrong?
Return data as list
return [data]