title: Web Server: socket programming for TCP connections in Python
body: <p>I'm trying to run a python web server. The server says is running so I assume it is all working, except that I can only see the html text. The jpeg/image and the pdf files won't dispaly. Here is what i have so far.</p>
  
  <pre><code>#import socket module
  from socket import *
  
  serverSocket = socket(AF_INET, SOCK_STREAM)
  serverSocket.bind(('', 12000))  
  serverSocket.listen(1) 
  
  while True:
      print 'Ready to serve...'    
      connectionSocket, addr = serverSocket.accept() 
      print 'Required connection', addr
  
      try:
          message = connectionSocket.recv(1024) 
          filename = message.split()[1]
          f = open(filename[1:])
          outputdata = f.read() 
  
          #Fill in start
          connectionSocket.send('HTTP/1.0 200 OK\r\n\r\n')
          #Fill in end
          #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:
          #Send response message for file not found
          #Fill in start
          connectionSocket.send('404 Not Found')                       
          #Fill in end
          #Close client socket
          #Fill in start
          connectionSocket.close()                      
          #Fill in end
          serverSocket.close()
  </code></pre>