The response header should contain a response code indicating success. Before the Content-Type line, add:
client.send('HTTP/1.0 200 OK\r\n')
In addition, to make the test more visible, put some content on the page:
client.send('<html><body><h1>Hello World</body></html>')
After sending the response, close the connection with:
client.close()
and
sock.close()
As other posters noted, end each line \r\n instead of \n .
Will these additions, I was able to run a successful test. In the browser, I entered localhost:8080 .
Here is the whole code:
import socket sock = socket.socket() sock.bind(('', 8080)) sock.listen(5) client, adress = sock.accept() print "Incoming:", adress print client.recv(1024) print client.send('HTTP/1.0 200 OK\r\n') client.send("Content-Type: text/html\r\n\r\n") client.send('<html><body><h1>Hello World</body></html>') client.close() print "Answering ..." print "Finished." sock.close()
Raymond hettinger
source share