title: Web Server & Socket Programming
body: <p>Currently doing an assignment in which we are programming sockets in python and thus creating a web server when the webserver.py code is executed.The code should then display HTTP headers and other information when you access a file(test.html)from the web server. Now my code works (or I'd like to believe so) and I have created a test.html file and the question goes on to say that I should place the test.html file in the same directory as the web server, where exactly is that on my local machine? I placed the test.html in the same folder as webserver.py in the python's root directory and proceeded to 127.0.0.1:1336/test.html to test my code but it doesn't work, where exactly on my machine is the webserver directory in which I should place test.html? Is it that I have to use wamp/xamp and place the test.html in there?</p>
  
  <p>N.B 1336 is the port I specified in the code to connect to.</p>
  
  <pre><code>  #import socket module
  from socket import *
  serverSocket = socket(AF_INET, SOCK_STREAM)
  #Prepare a sever socket
  serverPort = 1336
  serverSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
  serverSocket.bind(('', serverPort)) #set up socket connection
  
  
  serverSocket.listen(1) #tells the server to try a maximum of one connect request before ending connection
  
  
  
  while True:
      #Establish the connection
      print 'Ready to serve...'
  
  
      connectionSocket, addr = serverSocket.accept()
      print 'connected to port',serverPort
  
  
  
  try:
  
  
              message = connectionSocket.recv(1024) #Makes it so that you can recieve message from client
  
  
              filename = message.split()[1]
              f = open(filename[1:])
  
              outputdata = f.open(filename[1:])
  
              #Send one HTTP header line into socket2
              #Fill in start
              connectionSocket.send('HTTP/1.0 200 OK\r\n')
  
  
  
              #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
  
      print '404 Error : File Not Found.'
  
      #Close client socket
  
      connectionSocket.close()
  
  serverSocket.close()
  </code></pre>