Advertisement
Guest User

Untitled

a guest
Dec 15th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. import socket
  2. import sqlite3
  3. import thread
  4.  
  5. ip = "192.168.2.106"
  6. #ip = "10.2.2.179"
  7. port = 8888
  8.  
  9. LOG_IN = "101"
  10. LOG_IN_RES = "102"
  11. ECHO = "103"
  12.  
  13. def main():
  14. servSoc = socket.socket()
  15. servSoc.bind((ip, port))
  16.  
  17. i = 0
  18. while True:
  19. i+=1
  20. servSoc.listen(1)
  21. client_soc, client_addr = servSoc.accept()
  22. thread.start_new_thread(serve, (client_soc,))
  23. print i
  24.  
  25.  
  26. def serve(soc):
  27. protocol = {LOG_IN : log_in}
  28. while True:
  29. try:
  30. msg = soc.recv(1024)
  31. if msg[0:3] == LOG_IN:
  32. protocol[LOG_IN](soc, msg[3:])
  33. else:
  34. send(soc, LOG_IN_RES, "-1")
  35. except:
  36. return None
  37.  
  38.  
  39. def log_in(soc, data):
  40. db = sqlite3.connect("myDB.db")
  41. curr = db.cursor()
  42. name = data[2 : int(data[0:2]) + 2]
  43. password = data[int(data[0:2]) + 4 : ]
  44. curr.execute("SELECT * FROM Users WHERE Username = \'" + name + "\'AND Password = \'" + password + "\'")
  45. rows = curr.fetchall()
  46.  
  47. curr.close()
  48. if len(rows) is not 0:
  49. res = "1"
  50. else:
  51. res = "0"
  52. send(soc, LOG_IN_RES, res)
  53.  
  54. def send(soc, msg_type, msg):
  55. soc.send(msg_type + msg)
  56.  
  57. if __name__ == "__main__":
  58. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement