I am trying to create an HTTP server using python. The fact is that I get everything to work, except to send a response message; if the message has the text http , send() does not work.
Here is the code snippet:
connectionSocket.send('HTTP/1.1 200 OK text/html')
Here are the others I've tried:
connectionSocket.send(''.join('%s 200 OK text/html' % ('HTTP/1.1'))) connectionSocket.send('%s 200 OK text/html' % ('HTTP/1.1')) msg = 'HTTP/1.1 200 OK text/html' for i in range(0, len(msg)) connectionSocket.send(msg[i])
The only thing that seems to work is the entity associated with any character in http , e.g.
connectionSocket.send('HTTP/1.1 200 OK text/html')
Where H equivalent to H Otherwise, the browser does not display the header received from the python server socket.
The problem also occurs when I try to send a 404 Message on a socket. Other content is displayed, however, as an html file sent over a socket.
I want to know if there is a way to do this? Because if the client is not a browser, the html object will not be understood.
Thanks in advance
Update:
the code:
from socket import * serverSocket = socket(AF_INET, SOCK_STREAM) serverSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) serverSocket.bind(('127.0.0.1', 1240)) serverSocket.listen(1); while True: print 'Ready to serve...' connectionSocket, addr = serverSocket.accept() try: message = connectionSocket.recv(1024) filename = message.split()[1] f = open(filename[1:]) outputdata = f.read()
serverSocket.close ()
Screenshots:
Text as "HTTP / 1.1 ..."


Text as "HTTP / 1.1 ..."


HTML code hello.html
<html> <head> <title>Test Python</title> </head> <body> <h1>Hello World!</h1> </body> </html>
Gopikrishna s
source share