OkiBry

serverTest

May 27th, 2021
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. from _thread import *
  2. import socket
  3. import sys
  4. import time
  5. import sqlite3
  6. from sqlite3 import *
  7. import datetime
  8. import json
  9. database = 'database.db'
  10.  
  11.  
  12. PORT=8080
  13. n_client=2
  14. c_client=0
  15. connSet=set()
  16.  
  17. def addAcc(username: str, password: str, Type: int):
  18. try:
  19. connection = connect(database)
  20. cursor = connection.cursor()
  21. if Type==0:
  22. cursor.execute('''INSERT INTO account(user, pass) VALUES (?, ?)''',(username, password))
  23. else:
  24. cursor.execute('''INSERT INTO admin(user, pass) VALUES (?, ?)''',(username, password))
  25. connection.commit()
  26. cursor.close()
  27. connection.close()
  28. return (b'1')
  29. except:
  30. return (b'0')
  31. def addDate(ctID,date,tmp1,tmp2,sID):
  32. connection = connect(database)
  33. cursor = connection.cursor()
  34. try:
  35. cursor.execute('INSERT INTO info VALUES (?, ?, ?, ?, ?)',(ctID,date,tmp1,tmp2,sID))
  36. except:
  37. cursor.execute('UPDATE info SET tempFrom=?,tempTo=?,statusID=? WHERE id=? AND date=?',(tmp1,tmp2,sID,ctID,date))
  38. finally:
  39. connection.commit()
  40. cursor.close()
  41. connection.close()
  42.  
  43. def addCity(ID,name):
  44. try:
  45. connection = connect(database)
  46. cursor = connection.cursor()
  47. cursor.execute('INSERT INTO city VALUES (?, ?)',(ID,name))
  48. connection.commit()
  49. cursor.close()
  50. connection.close()
  51. return b'1'
  52. except:
  53. return b'0'
  54. def Login(username: str,password: str, Type:int):
  55. connection = connect(database)
  56. cursor = connection.cursor()
  57. if Type==0:
  58. cursor1=cursor.execute('SELECT * FROM account WHERE user = ? AND pass = ?',(username,password)).fetchall()
  59. else :
  60. cursor1=cursor.execute('SELECT * FROM admin WHERE user = ? AND pass = ?',(username,password)).fetchall()
  61. cursor.close()
  62. connection.close()
  63. if not cursor1:
  64. return b'0'
  65. else:
  66. return b'1'
  67. def getAll(date):
  68. connection = connect(database)
  69. cursor = connection.cursor()
  70. cursor1=cursor.execute('''SELECT * FROM info JOIN city ON info.id=city.id WHERE date = ? ''',(date,)).fetchall()
  71. cursor.close()
  72. connection.close()
  73. return cursor1
  74. def getDate(ID,date):
  75. connection = connect(database)
  76. cursor = connection.cursor()
  77. cursor1=cursor.execute('''SELECT * FROM info,city WHERE info.id = ? AND date=? AND info.id=city.id ''',(ID,date)).fetchall()
  78. cursor.close()
  79. connection.close()
  80. return cursor1
  81. def endAll():
  82. global c_client
  83. for conn in connSet:
  84. connSet.discard(conn)
  85. conn.close()
  86. c_client=c_client-1
  87.  
  88.  
  89. def clientthread(conn,addr):
  90. while True:
  91. data=""
  92. try:
  93. data=conn.recv(1024)
  94. except:
  95. print (addr[0]+':'+str(addr[1])+' ngat ket noi')
  96. break
  97. if not data:
  98. print (addr[0]+':'+str(addr[1])+' ngat ket noi')
  99. break
  100. data=data.decode()
  101. print(data)
  102.  
  103. x=data.split(" ")
  104. if x[0]=="acc":
  105. if x[3]=="0": #user
  106. conn.sendall(Login(x[1],x[2],0))
  107. else: #admin
  108. conn.sendall(Login(x[1],x[2],1))
  109. if x[0]=="re":
  110. conn.sendall(addAcc(x[1],x[2],0))
  111. if x[0]=="get":
  112. if x[1]=="all":
  113. if x[2]=="now":
  114. date=datetime.datetime.now()
  115. x[2]=str(date.day)+"/"+str(date.month)+"/"+str(date.year)
  116. x[2]="20/5/2021"
  117. jsend={"all" :[]}
  118. data=getAll(x[2])
  119. for line in data:
  120. tmpInfo={}
  121. tmpInfo["id"]=line[0]
  122. tmpInfo["name"]=line[6]
  123. tmpInfo["date"]=line[1]
  124. tmpInfo["tmpF"]=line[2]
  125. tmpInfo["tmpT"]=line[3]
  126. tmpInfo["statusID"]=line[4]
  127. jsend["all"].append(tmpInfo)
  128. jsended=json.dumps(jsend)
  129. conn.sendall(jsended.encode())
  130. else:
  131. jsend={"7days":[]}
  132. for i in range(7):
  133. date=datetime.datetime.today()+datetime.timedelta(days=i)
  134. toDay=str(date.day)+"/"+str(date.month)+"/"+str(date.year)
  135. toDay='15/5/2021'
  136. data=getDate('82',toDay)
  137. tmpInfo={}
  138. tmpInfo["id"]=data[0][0]
  139. tmpInfo["name"]=data[0][6]
  140. tmpInfo["date"]=data[0][1]
  141. tmpInfo["tmpF"]=data[0][2]
  142. tmpInfo["tmpT"]=data[0][3]
  143. tmpInfo["statusID"]=data[0][4]
  144. jsend["7days"].append(tmpInfo)
  145. jsended=json.dumps(jsend)
  146. conn.sendall(jsended.encode())
  147. if x[0]=="end": #client end
  148. conn.sendall(b'ok end')
  149. break
  150. if x[0]=="admin":
  151. if x[1]=="addCity":
  152. tmpx=data.split(" ",3)
  153. conn.sendall(addCity(x[2],tmpx[3]))
  154. if x[1]=="upDate":
  155. addDate(x[2],x[3],x[4],x[5],x[6])
  156. conn.sendall(b'1')
  157. if x[1]=="upCity":
  158. s7days=data.partition("*")[2]
  159. s7days=s7days.split("\n")
  160. s7days.pop()
  161. for line in s7days:
  162. print (line)
  163. line=line.split(" ")
  164. addDate(x[2],line[0],line[1],line[2],line[3])
  165. conn.sendall(b'1')
  166. global c_client
  167. global connSet
  168. c_client=c_client-1
  169. connSet.discard(conn)
  170. conn.close()
  171. def main():
  172. try:
  173. hostname=socket.gethostname()
  174. local_ip = socket.gethostbyname(hostname)
  175. HOST=str(local_ip)
  176. print(HOST)
  177.  
  178. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  179. s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
  180. s.bind((HOST, PORT))
  181. print ("Listening on %s %d" %(HOST, (PORT)))
  182. s.listen(2)
  183.  
  184. global connSet
  185. global c_client
  186.  
  187. while True:
  188. if c_client==n_client:
  189. continue
  190. conn, addr = s.accept()
  191. print ('Connected by ' + addr[0] + ':' + str(addr[1]))
  192. c_client=c_client+1
  193. connSet.add(conn)
  194. start_new_thread(clientthread,(conn,addr))
  195. s.close()
  196. except KeyboardInterrupt as msg:
  197. sys.exit(0)
  198.  
  199. if __name__=="__main__":
  200. main()
  201.  
Advertisement
Add Comment
Please, Sign In to add comment