Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. SERVER:
  2.  
  3. from socket import *
  4. import threading
  5. from threading import *
  6.  
  7. condition = threading.Condition()
  8. SERVER_ADDRESS = ('127.0.0.1', 5053)
  9. client_list = []
  10. client_list_mes = []
  11. word = 0
  12. id = 0
  13. room = []
  14. count = 0
  15. wordList = ["bow", "alligator", "curious", "drowsy", "helmet", "ironic", "glacier", \
  16. "glamor", "trident", "adventure", "unknown", "companion", "dilemma", \
  17. "fragment", "happiness", "birthday", "internet", "technology", "discovery", \
  18. "cryptic", "chamber", "universe", "mantis", "barn", "biscuit", "eagle", "stamp", \
  19. "phantom", "puppy", "aquatic", "mythical", "compose", "fortress", "ice", \
  20. "consequence", "ogre", "brigade", "flamethrower", "amulet", "corrupt", "donkey", \
  21. "radioactive", "parasite", "exchange", "blind", "slumber", "comet", "ceremony", \
  22. "definite", "galaxy", "mutation", "aroma", "morning", "theater", "blizzard", \
  23. "belgium", "evidence", "sibling", "electricity", "volume", "clinic", "elephant", \
  24. "submarine", "blade", "orbit", "obscure", "vision", "penguin", "luxury", "swamp", \
  25. "knuckles", "legendary", "torment", "plague", "shovel", "metal", "flag", \
  26. "badge", "rabbit", "nature", "valve", "version", "dough", "realm", "corridor", \
  27. "capacity", "depth", "monster", "tongue", "fountain", "pressure", "hazard", "skill", \
  28. "crowd", "gadget", "random", "generator", "narrator", "triangle", "pizza", "wonderful"]
  29.  
  30.  
  31. class ProducerThread(threading.Thread):
  32. def __init__(self, client, addr, username):
  33. self.client = client
  34. self.addr = addr
  35. self.username = username
  36.  
  37. threading.Thread.__init__(self)
  38.  
  39. def run(self):
  40. global client_list_mes
  41. while True:
  42. mess = self.client.recv(1024)
  43. condition.acquire()
  44. client_list_mes.append((mess, self.addr))
  45. print (mess, self.addr)
  46.  
  47. condition.notify()
  48. condition.release()
  49.  
  50.  
  51. class ConsumerThread(threading.Thread):
  52. def __init__(self):
  53.  
  54. threading.Thread.__init__(self)
  55.  
  56. def run(self):
  57. global client_list_mes
  58. global client_list
  59. while True:
  60. condition.acquire()
  61. if not client_list_mes:
  62. condition.wait()
  63. m = client_list_mes.pop()
  64. for client in client_list:
  65. if client != self.client:
  66. client.send(m)
  67. condition.release()
  68.  
  69.  
  70. class Session_with_client(threading.Thread):
  71. def __init__(self, clientsock, addr):
  72. self.client = clientsock
  73. self.clientaddr = addr
  74. threading.Thread.__init__(self)
  75.  
  76. def run(self):
  77. while True:
  78. m = self.client.recv(1024)
  79. print m
  80. for i in client_list:
  81. if i[0] is not self.client:
  82. i[0].send("found" + str(self.clientaddr))
  83.  
  84. class LogIn(threading.Thread):
  85. def __init__(self, clientsock, addr):
  86. self.clientsock = clientsock
  87. self.addr = addr
  88. threading.Thread.__init__(self)
  89.  
  90. def run(self):
  91. global client_list
  92. global word
  93. global id
  94. global wordList
  95. global room
  96. global count
  97.  
  98. username = ""
  99. print "Waiting for input...\n"
  100. username = self.clientsock.recv(1024)
  101.  
  102. if count == 0 or count == 1:
  103. room.append((self.clientsock, self.addr, username))
  104. count +=1
  105. self.clientsock.send("wait for more players")
  106.  
  107. elif len(room) == 2:
  108. print "2222"
  109. room.append((self.clientsock, self.addr, username))
  110. for client in room:
  111. client[0].send(wordList[0])
  112. client_list.append(room)
  113. room = []
  114. count = 0
  115.  
  116.  
  117.  
  118. # if len(room) <= 2:
  119. # print "Waiting for input...\n"
  120. # username = self.clientsock.recv(1024)
  121. # print username
  122. # room.append((self.clientsock, self.addr, username))
  123. #
  124. # else:
  125. # client_list.append(room)
  126. #
  127. # for client in room:
  128. # client[0].send(wordList[0])
  129. # room = []
  130.  
  131.  
  132. print client_list
  133.  
  134. p = ProducerThread(self.clientsock, self.addr, username)
  135. p.start()
  136. c = ConsumerThread()
  137. c.start()
  138.  
  139.  
  140. class Server(object):
  141. def __init__(self):
  142. self.socket = socket(AF_INET, SOCK_STREAM)
  143. self.socket.bind(SERVER_ADDRESS)
  144. self.socket.listen(3) # The serversock is listening to requests
  145.  
  146. def run_server(self):
  147. global client_list
  148. global word
  149. global id
  150. global wordList
  151. while 1:
  152. print 'Waiting for connection...'
  153. clientsock, addr = self.socket.accept()
  154. print ('Connected from: ', addr)
  155. l = LogIn(clientsock, addr)
  156. l.start()
  157.  
  158.  
  159. s = Server()
  160. s.run_server()
  161.  
  162. # cd PycharmProjects\untitled28
  163.  
  164. ---------------------------------------------------------------------------------------------------------------------------
  165. ---------------------------------------------------------------------------------------------------------------------------
  166. CLIENT:
  167.  
  168. from socket import *
  169. import threading
  170.  
  171. HOST = '127.0.0.1'
  172. PORT = 5053
  173. ADDR = (HOST, PORT)
  174. id = ""
  175.  
  176.  
  177. class sed(threading.Thread):
  178. global id
  179.  
  180. def __init__(self, soc):
  181. self.sir = soc
  182. threading.Thread.__init__(self)
  183.  
  184. def run(self):
  185. while True:
  186. mess = raw_input()
  187. self.sir.send(str(id) + mess)
  188.  
  189.  
  190. class rec(threading.Thread):
  191. def __init__(self, soc):
  192. self.sir = soc
  193. threading.Thread.__init__(self)
  194.  
  195. def run(self):
  196. while True:
  197. print self.sir.recv(1024)
  198.  
  199.  
  200. class LogIn(threading.Thread):
  201. def __init__(self, soc):
  202. self.sir = soc
  203. threading.Thread.__init__(self)
  204.  
  205. def run(self):
  206. username = raw_input("Enter username...")
  207. self.sir.send(username)
  208.  
  209. r1 = sed(self.sir)
  210. r1.start()
  211. r2 = rec(self.sir)
  212. r2.start()
  213.  
  214.  
  215. class Session_with_server(threading.Thread):
  216. def __init__(self, serversock):
  217. self.serv = serversock
  218. threading.Thread.__init__(self)
  219.  
  220. def run(self):
  221. l = LogIn(self.serv)
  222. l.start()
  223.  
  224.  
  225. class MyClient(object):
  226. def __init__(self):
  227. self.sock = socket()
  228.  
  229. def get_connection(self):
  230. global id
  231. self.sock.connect(ADDR)
  232. # id = self.sock.recv(1024)
  233. # print id
  234. s = Session_with_server(self.sock)
  235. s.start()
  236.  
  237.  
  238. c = MyClient()
  239. c.get_connection()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement