Reputation: 2431
I am trying to create a HTTP server using python. The thing is I am getting everything to work except for sending a response message; if the message has a text http
, the send()
doesn't work.
Here is the snippet of the code:
connectionSocket.send('HTTP/1.1 200 OK text/html')
Here are the others I 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 entity-fying the any of the character in HTTP
, like
connectionSocket.send('HTTP/1.1 200 OK text/html')
Where H
is equivalent to H
. Otherwise the browser doesn't display the header received from the python server socket.
The problem also goes when I am trying to send a 404 Message
down the socket. The other contents are displayed, however, like a html file sent through the socket.
I want to know is there a proper way to do it? Because, if the client is not a browser, the html entity will not be understood.
Thanks in advance
Update:
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()
#Send one HTTP header line into socket
connectionSocket.send('HTTP/1.1 200 OK text/html') ## this is not working
#Send the content of the requested file to the client
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i])
connectionSocket.close()
except IOError:
connectionSocket.send('HTTP/1.1 404 File not found') ## this is not working
connectionSocket.close();
serverSocket.close()
Screenshots:
Text as 'HTTP/1.1 ...'
Text as 'HTTP/1.1 ...'
HTML Code of hello.html
<html>
<head>
<title>Test Python</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Upvotes: 8
Views: 14962
Reputation: 37003
You are not returning a correctly formed HTTP response. Your line
connectionSocket.send('HTTP/1.1 200 OK text/html') ## this is not working
is not even terminated by a newline, then immediately followed by the content of your file. Protocols like HTTP specify fairly rigorously what must be sent, and I find it little short of miraculous that you saw anything at all in your browser.
Try something like:
connectionSocket.send('HTTP/1.1 200 OK\nContent-Type: text/html\n\n')
This is the start of a correctly-formed HTTP 1.1 response with a primary response line and a single header. The double newline terminates the headers, preparing the client to read the content that follows.
http://www.jmarshall.com/easy/http/ is one of many approachable ways to learn a bit more about the protocol you have chosen to use. Good luck!
Upvotes: 10
Reputation: 59416
I'm not sure what connectionSocket
you are using (which module, library, etc.) but if this thing is already part of a HTTP-related routine, it might well be that it already sends the necessary HTTP
line without your doing. Yours then might disturb the process.
The quoted version (HTTP
...) probably is not recognized by the HTTP protocol in the browser (I think that quoting is only recognized and interpreted in higher layers of the OSI stack) and therefore does not have the same effect.
Upvotes: 0