Reputation: 131
I am trying to display text/html/css/images on my browser with a python web server. But only the css/text/html is working. Anyone knows why the images are not showing?
Here is my code :
import urlparse
from socket import *
s = socket(AF_INET, SOCK_STREAM)
port = 8080
s.bind(('', port))
s.listen(1)
#Fill in end
while True:
client, addr = s.accept()
try:
data = client.recv(1024)
filename = data.split()[1]
file = open(filename[1:])
outputdata = file.read()
client.send('\nHTTP / 1.x200OK\n')
for i in range(0, len(outputdata)):
client.send(outputdata[i])
client.close()
except IOError:
client.send('\n404 File Not Found\n')
client.close()
s.close()
Thank you!
Upvotes: 1
Views: 973
Reputation: 103
The problem is likely that you're not setting the right HTTP headers (Content-Type, Content-Length, etc). Instead of re-implementing this stuff from scratch, why not use one of the many Python web frameworks that will take care of all this for you?
Upvotes: 1