title: Send text "http" over python socket
body: <p>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 <code>http</code>, the <code>send()</code> doesn't work.</p>
  
  <p>Here is the snippet of the code:</p>
  
  <pre><code>connectionSocket.send('HTTP/1.1 200 OK text/html')
  </code></pre>
  
  <p>Here are the others I tried:</p>
  
  <pre><code>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])
  </code></pre>
  
  <p>The only thing that seems to work is entity-fying the any of the character in <code>HTTP</code>, like</p>
  
  <pre><code>connectionSocket.send('&amp;#72;TTP/1.1 200 OK text/html')
  </code></pre>
  
  <p>Where <code>&amp;#72;</code> is equivalent to <code>H</code>. Otherwise the browser doesn't display the header received from the python server socket.</p>
  
  <p>The problem also goes when I am trying to send a <code>404 Message</code> down the socket. The other contents are displayed, however, like a html file sent through the socket.</p>
  
  <p>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.</p>
  
  <p>Thanks in advance</p>
  
  <p>Update:</p>
  
  <p>Code:</p>
  
  <pre><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('&amp;#72;TTP/1.1 404 File not found') ## this is not working
      connectionSocket.close();
  </code></pre>
  
  <p>serverSocket.close()</p>
  
  <p>Screenshots:</p>
  
  <p>Text as 'HTTP/1.1 ...'</p>
  
  <p><img src="https://i.stack.imgur.com/Nd1IP.png" alt="enter image description here"></p>
  
  <p><img src="https://i.stack.imgur.com/MWivA.png" alt="enter image description here"></p>
  
  <p>Text as '&#72;TTP/1.1 ...'</p>
  
  <p><img src="https://i.stack.imgur.com/yd8W7.png" alt="enter image description here"></p>
  
  <p><img src="https://i.stack.imgur.com/BwdGF.png" alt="enter image description here"></p>
  
  <p>HTML Code of hello.html</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;Test Python&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;h1&gt;Hello World!&lt;/h1&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>