Guest User

Untitled

a guest
Jun 22nd, 2011
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. from socket import *
  2. import time, thread, random
  3. Clients, Clientsonline, Port, Port2, dummy, HostIP = {}, [], 50005, 50003, "", "107.107.107.107"
  4.  
  5. ###################################### DATABASE ################################
  6.  
  7. def writedatabase():
  8. try:
  9. f = open("database", "r+")
  10. except:
  11. f = open("database", "w")
  12. f.close()
  13. print "database created"
  14. f = open("database", "r+")
  15. for element in Clients:
  16. savestring = ""
  17. for item in Clients[element][0]:
  18. savestring = "%s%s " % (savestring, item)
  19. savestring = "%s\n" % savestring
  20. f.write(savestring)
  21. f.close()
  22. print "database saved"
  23.  
  24. def readdatabase():
  25. try:
  26. f = open("database", "r+")
  27. for line in f:
  28. g = line.split()
  29. name = g[0]
  30. Clients[name] = [[name], ["conn", "addr"], ["conn2", "addr2"]]
  31. g.pop(0)
  32. for element in g:
  33. Clients[name][0].append(element)
  34. except:
  35. print "couldnt read database"
  36.  
  37. def saveloop(dummy):
  38. while 1:
  39. time.sleep(120)
  40. writedatabase()
  41.  
  42. ####################################### COMMUNICATION ##########################
  43.  
  44. def mainloop(dummy):
  45. soc = socket(AF_INET, SOCK_STREAM)
  46. soc.bind((HostIP, Port))
  47. soc.listen(100)
  48. while 1:
  49. conn, addr = soc.accept()
  50. thread.start_new_thread(passwordthread, (conn, addr))
  51.  
  52. def passwordthread(conn, addr):
  53. while 1:
  54. name = conn.recv(1024)
  55. name2 = name.split()
  56. name, password = name2[0], name2[1]
  57. aaa = name in Clients
  58. if aaa == True:
  59. if password == Clients[name][0][1]:
  60. Clients[name] = [[name, password], [conn, addr], ["conn2", "addr2"]]
  61. print "%s connected with password %s" %(name, password)
  62. conn.send("welcome")
  63. Clientsonline.append(name)
  64. break
  65. if password != Clients[name][0][1]:
  66. conn.send("wrong password")
  67. if aaa == False:
  68. Clients[name] = [[name, password], [conn, addr], ["conn2", "addr2"]]
  69. print "%s connected with password %s" %(name, password)
  70. conn.send("welcome")
  71. Clientsonline.append(name)
  72. break
  73. soc2 = socket(AF_INET, SOCK_STREAM)
  74. soc2.bind((HostIP, Port2))
  75. soc2.listen(100)
  76. conn2, addr2 = soc2.accept()
  77. Clients[name][2]=[conn2, addr2]
  78. thread.start_new_thread(sendloop, (name, conn, addr))
  79. thread.start_new_thread(recvloop, (name, conn2, addr2))
  80.  
  81. def sendloop(name, conn, addr):
  82. try:
  83. while 1:
  84. conn.send("server ping")
  85. time.sleep(15)
  86. except:
  87. bbb = Clientsonline.remove(name)
  88.  
  89. def recvloop(name, conn2, addr2):
  90. try:
  91. while 1:
  92. data = conn2.recv(1024)
  93. print name, "says", data
  94. except:
  95. print name, "has disconnected"
  96.  
  97. #################################### ACTION ####################################
  98.  
  99. readdatabase()
  100. thread.start_new_thread(mainloop, (dummy, ))
  101. thread.start_new_thread(saveloop, (dummy, ))
  102. while 1:
  103. print Clientsonline
  104. time.sleep(15)
  105. pass
Advertisement
Add Comment
Please, Sign In to add comment