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>
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With