I am attempting to display an image on localhost
. As a first step, I have created a sever script in python
#!/usr/bin/env python
import BaseHTTPServer
import CGIHTTPServer
import cgitb; cgitb.enable() ## This line enables CGI error reporting
server = BaseHTTPServer.HTTPServer
handler = CGIHTTPServer.CGIHTTPRequestHandler
server_address = ("", 8000)
handler.cgi_directories = ["/"]
httpd = server(server_address, handler)
httpd.serve_forever()
The image is placed within the same directory where this script is executing.
Subsequently http://localhost:8000/test.jpg
is typed in the browser.
The browser fails to render the image with an error:
The image "http://localhost:8000/test.jpg" cannot be displayed because it contains errors.
The server script throws an error like
File "/usr/lib/python2.7/CGIHTTPServer.py", line 253, in run_cgi
os.execve(scriptfile, args, env)
OSError: [Errno 8] Exec format error
I have tried displaying text and the server works fine with lot of examples. Except that it fails to load images. Where am I going wrong?
The problem was solved. I moved the test.jpg into a sub-directory within the server directory.
Your code is attempting to execute the test.jpg as a cgi script. If you remove the CGIHttpRequestHandler and instead use SimpleHTTPServer.SimpleHTTPRequestHandler you will get your image back. If you need both, then you need to put the image somewhere else.