Advertisement
Guest User

Untitled

a guest
May 13th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.90 KB | None | 0 0
  1. # coding: utf-8
  2. import datetime
  3. import string,cgi,time
  4. from os import curdir, sep
  5. from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
  6. #import pri
  7.  
  8. class MyHandler(BaseHTTPRequestHandler):
  9.    
  10.     print "wrking"
  11.     book = {'andrew':'test'}
  12.     language = ["English","French","Italian","Russian"];
  13.     translations =  [
  14.                      ["language","military","economy","culture","country","member","design","study","activity","sound","music","house","war","peace","air","water","land","correct","long","word"],
  15.                      ["langage","militaire","économie","culture","pays","membre","conception","étude","activité","bruit","musique","maison","guerre","paix","air","leau","terre","correct","longtemps","mot"],
  16.                      ["linguaggio","militare","economia","cultura","paese","membro","disegno","studio","attività","suono","musica","casa","guerra","pace","aria","acqua","terra","corretto","lungamente","parola"],
  17.                      ["язык","воинско","экономия","культура","страна","член","конструкция","изучение","деятельность","звук","нот","дом","война","мир","воздух","вода","земля","правильно","длиной","слово"]
  18.                     ]
  19.    
  20.     rating = []
  21.     for i in range(20):
  22.         rating.append([])
  23.             for j in range(4):
  24.                 rating[i].append(0)
  25.  
  26.     user = []
  27.     for i in range(20):
  28.         user.append([])
  29.             for j in range(4):
  30.                 user[i].append("default")
  31.     now = datetime.datetime.now()
  32.     time = []
  33.     for i in range(20):
  34.         time.append([])
  35.             for j in range(4):
  36.                 time[i].append(str(now.hour)+":"+str(now.minute)+":"+str(now.second))
  37.    
  38.    
  39.    
  40.            
  41.     currentUser = "default"
  42.     def do_GET(self):
  43.         global translations
  44.         global rating
  45.         global user
  46.         global time
  47.         global currentUser
  48.         try:
  49.            
  50.             if (self.path.partition('?'))[2].startswith("CT"):
  51.                 subS = ((self.path.partition('?'))[2].partition("&"))[2]
  52.                 changeT = (subS.partition("="))[0]
  53.                 index1 = ((subS.partition("="))[2].partition("="))[0]
  54.                 index2 = ((((subS.partition("="))[2].partition("="))[2]).partition("&"))[0]
  55.                 translations[index1][index2] = changeT
  56.                 time[index1][index2] = ((((subS.partition("="))[2].partition("="))[2]).partition("&"))[2]
  57.                 user[index1][index2] = currentUser;
  58.                 rating[index1][index2] = 0;
  59.            
  60.             elif (self.path.partition('?'))[2].startswith("UR"):
  61.                 subString = ((self.path.partition('?'))[2].partition("&"))[2]
  62.                 changeR = int((subString.partition("="))[0])
  63.                 indexR1 = ((subString.partition("="))[2].partition("="))[0]
  64.                 indexR2 = ((subString.partition("="))[2].partition("="))[2]
  65.                 rating[indexR1][indexR2] = changeR
  66.                
  67.             else:
  68.                 #account = bufferstring
  69.                 #password = bufferstring2
  70.                 temp = (self.path.partition('?'))[2]
  71.                 account = (temp.partition('='))[0]
  72.                 password = (temp.partition('='))[2]
  73.            
  74.            
  75.                 #if account is in dictionary then u check if the password matches
  76.                 if book.has_key(account):
  77.                     if book[account] == password:
  78.                         returnvalue = "true"
  79.                         currentUser = account;
  80.                     else:
  81.                    
  82.                         returnvalue = "false"
  83.                 else:
  84.                     returnvalue="false"
  85.                
  86.                 self.send_response(200)
  87.                 self.send_header('Content-type','text/plain')
  88.                 self.end_headers()
  89.                 self.wfile.write(returnvalue)
  90.                
  91.         except IOError:
  92.                 self.send_error(404)
  93.      
  94.  
  95.     def do_POST(self):
  96.         global translations, rating, user, time
  97.         returnS = ""
  98.         for x in range(20):
  99.             for y in range(4):
  100.                 print str(x)+"  "+str(y)
  101.                 returnS += (translations[y][x])+"&"+str(rating[x][y])+"&"+user[x][y]+"&"+time[x][y]+"&"
  102.         self.send_response(200)
  103.         self.send_header('Content-type','text/plain')
  104.         self.end_headers()
  105.         self.wfile.write(returnS)
  106.        
  107.  
  108. def main():
  109.     try:
  110.         server = HTTPServer(('', 2181), MyHandler)
  111.         print 'started httpserver...'
  112.         server.serve_forever()
  113.     except KeyboardInterrupt:
  114.         print '^C received, shutting down server'
  115.         server.socket.close()
  116.  
  117. if __name__ == '__main__':
  118.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement