import socket
import sys,time,datetime,string, os

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#localhost port 5555
s.bind(('',5555))
s.listen(1)

#http header actions
firstheader ='HTTP/1.0 200 OK\r\nServer: Custom\r\nDate:   \r\nContent-Type: text/html\r\nConnection: Closed\r\n\r\n'
imageheader ='HTTP/1.1 200 OK\r\nx-content-type-options:nosniff\r\nContent-Type: image/jpeg\r\nContent-Length: 59383\r\nConnection: Keep-Alive\r\n\r\n'

#NOTE: all print statements show up only on server *console*
#determine if file exists or not
def isfileexist(fname):
  if os.path.isfile(fn) == False:
      return False
  else:
      return True

def buildresponse(fname):
    #get length of file in bites

    #split the extension
    sfex = fname.split('.')
    ex = sfex[1]
    if ex == 'jpg':
       h = 'image/jpeg'
    if ex == 'png':
       h = 'image/png'
    if ex == 'gif':
       h = 'image/gif'
    if ex == 'html':
       h = 'text/html'

    if ismediafile(fname)== True:
      i = open(fname,'rb').read()
      bytelen = len(i)
      k = 'Content-Length: '+str(bytelen)
    else:
      k = ''

    strheader ='HTTP/1.1 200 OK\r\nx-content-type-options:nosniff\r\nContent-Type: '+str(h)+ '\r\n'+k+'\r\nConnection: Keep-Alive\r\n\r\n'
    return strheader

def ismediafile(fname):
    sfex = fname.split('.')
    ex = sfex[1]
    if ex == 'jpg':
       return True
    elif ex == 'png':
       return True
    elif ex == 'gif':
       return True
    else:
       return False

while True:

      conn, addr = s.accept()

      data = conn.recv(1024)

      #print data
      #split up parts of the contents in list/array
      r = data.split()
      f = r[1]
      #split / from url
      fn = f[1:len(f)]

      print "output"+fn
      #normal http GET resquests
      if r[0] == 'GET':
      #get file from GET header

         #print isfileexist(f)

         #if no defaultfile dish out index file
         if f == '/':
             u = open('index.html','r').read()
             conn.send(firstheader+str(u))
         else:
           #if file exists, dish out error file
           if isfileexist(f) == False:
               #trigger error file
               u = open('error.html','r').read()
               conn.send(firstheader+str(u))

           #if file does exist, open the file and pipe it to user
           if isfileexist(f) == True:
              #print ">>>>>>>>>>"+buildresponse(fn)

              if ismediafile(fn) == True:
                  u = open(fn,'rb').read()
                  conn.send(buildresponse(fn)+str(u))
              else:
                   u = open(fn,'r').read()
                   conn.send(firstheader+str(u))

           #######################
           #shuts server down
           ########################
           if f == '/shutdown.html':

              print "server has been shutdown"
              sys.exit()
              break

      #handle post requests, when someone submits a form
      if r[0] == 'POST':

         a = r.index("Content-Length:")+2
         qs = r[a]

         #put POST query into a dictionary
         d = dict([x.split("=") for x in qs.split("&")])

         #print out the variables from post
         #print "USERNAME: "+ d['user']
         #print "PASSWORD: "+ d['pass']

         u = open(fn,'r').read()
         conn.send(firstheader+str(u))

      conn.close()
      print "connect closed...still listening"
Advertisements