Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SERVER:
- from socket import *
- import threading
- from threading import *
- condition = threading.Condition()
- SERVER_ADDRESS = ('127.0.0.1', 5053)
- client_list = []
- client_list_mes = []
- word = 0
- id = 0
- room = []
- count = 0
- wordList = ["bow", "alligator", "curious", "drowsy", "helmet", "ironic", "glacier", \
- "glamor", "trident", "adventure", "unknown", "companion", "dilemma", \
- "fragment", "happiness", "birthday", "internet", "technology", "discovery", \
- "cryptic", "chamber", "universe", "mantis", "barn", "biscuit", "eagle", "stamp", \
- "phantom", "puppy", "aquatic", "mythical", "compose", "fortress", "ice", \
- "consequence", "ogre", "brigade", "flamethrower", "amulet", "corrupt", "donkey", \
- "radioactive", "parasite", "exchange", "blind", "slumber", "comet", "ceremony", \
- "definite", "galaxy", "mutation", "aroma", "morning", "theater", "blizzard", \
- "belgium", "evidence", "sibling", "electricity", "volume", "clinic", "elephant", \
- "submarine", "blade", "orbit", "obscure", "vision", "penguin", "luxury", "swamp", \
- "knuckles", "legendary", "torment", "plague", "shovel", "metal", "flag", \
- "badge", "rabbit", "nature", "valve", "version", "dough", "realm", "corridor", \
- "capacity", "depth", "monster", "tongue", "fountain", "pressure", "hazard", "skill", \
- "crowd", "gadget", "random", "generator", "narrator", "triangle", "pizza", "wonderful"]
- class ProducerThread(threading.Thread):
- def __init__(self, client, addr, username):
- self.client = client
- self.addr = addr
- self.username = username
- threading.Thread.__init__(self)
- def run(self):
- global client_list_mes
- while True:
- mess = self.client.recv(1024)
- condition.acquire()
- client_list_mes.append((mess, self.addr))
- print (mess, self.addr)
- condition.notify()
- condition.release()
- class ConsumerThread(threading.Thread):
- def __init__(self):
- threading.Thread.__init__(self)
- def run(self):
- global client_list_mes
- global client_list
- while True:
- condition.acquire()
- if not client_list_mes:
- condition.wait()
- m = client_list_mes.pop()
- for client in client_list:
- if client != self.client:
- client.send(m)
- condition.release()
- class Session_with_client(threading.Thread):
- def __init__(self, clientsock, addr):
- self.client = clientsock
- self.clientaddr = addr
- threading.Thread.__init__(self)
- def run(self):
- while True:
- m = self.client.recv(1024)
- print m
- for i in client_list:
- if i[0] is not self.client:
- i[0].send("found" + str(self.clientaddr))
- class LogIn(threading.Thread):
- def __init__(self, clientsock, addr):
- self.clientsock = clientsock
- self.addr = addr
- threading.Thread.__init__(self)
- def run(self):
- global client_list
- global word
- global id
- global wordList
- global room
- global count
- username = ""
- print "Waiting for input...\n"
- username = self.clientsock.recv(1024)
- if count == 0 or count == 1:
- room.append((self.clientsock, self.addr, username))
- count +=1
- self.clientsock.send("wait for more players")
- elif len(room) == 2:
- print "2222"
- room.append((self.clientsock, self.addr, username))
- for client in room:
- client[0].send(wordList[0])
- client_list.append(room)
- room = []
- count = 0
- # if len(room) <= 2:
- # print "Waiting for input...\n"
- # username = self.clientsock.recv(1024)
- # print username
- # room.append((self.clientsock, self.addr, username))
- #
- # else:
- # client_list.append(room)
- #
- # for client in room:
- # client[0].send(wordList[0])
- # room = []
- print client_list
- p = ProducerThread(self.clientsock, self.addr, username)
- p.start()
- c = ConsumerThread()
- c.start()
- class Server(object):
- def __init__(self):
- self.socket = socket(AF_INET, SOCK_STREAM)
- self.socket.bind(SERVER_ADDRESS)
- self.socket.listen(3) # The serversock is listening to requests
- def run_server(self):
- global client_list
- global word
- global id
- global wordList
- while 1:
- print 'Waiting for connection...'
- clientsock, addr = self.socket.accept()
- print ('Connected from: ', addr)
- l = LogIn(clientsock, addr)
- l.start()
- s = Server()
- s.run_server()
- # cd PycharmProjects\untitled28
- ---------------------------------------------------------------------------------------------------------------------------
- ---------------------------------------------------------------------------------------------------------------------------
- CLIENT:
- from socket import *
- import threading
- HOST = '127.0.0.1'
- PORT = 5053
- ADDR = (HOST, PORT)
- id = ""
- class sed(threading.Thread):
- global id
- def __init__(self, soc):
- self.sir = soc
- threading.Thread.__init__(self)
- def run(self):
- while True:
- mess = raw_input()
- self.sir.send(str(id) + mess)
- class rec(threading.Thread):
- def __init__(self, soc):
- self.sir = soc
- threading.Thread.__init__(self)
- def run(self):
- while True:
- print self.sir.recv(1024)
- class LogIn(threading.Thread):
- def __init__(self, soc):
- self.sir = soc
- threading.Thread.__init__(self)
- def run(self):
- username = raw_input("Enter username...")
- self.sir.send(username)
- r1 = sed(self.sir)
- r1.start()
- r2 = rec(self.sir)
- r2.start()
- class Session_with_server(threading.Thread):
- def __init__(self, serversock):
- self.serv = serversock
- threading.Thread.__init__(self)
- def run(self):
- l = LogIn(self.serv)
- l.start()
- class MyClient(object):
- def __init__(self):
- self.sock = socket()
- def get_connection(self):
- global id
- self.sock.connect(ADDR)
- # id = self.sock.recv(1024)
- # print id
- s = Session_with_server(self.sock)
- s.start()
- c = MyClient()
- c.get_connection()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement