37,718
社区成员
发帖
与我相关
我的任务
分享
#!/usr/bin/python
"MyTools.py"
import time
import Queue
import threading
def clock():
strtime = time.ctime()
index = strtime.index(":")
return strtime[index-2:index+6]
class MyThread(threading.Thread):
def __init__(self, func, args):
threading.Thread.__init__(self)
self.func = func
self.args = args
def run(self):
apply(self.func, self.args)
class MyLock(object):
def __init__(self):
self.queue = Queue.Queue(1)
def lock(self):
self.queue.put("", True)
def unlock(self):
self.queue.get(True)
#!/usr/bin/python
"Server.py"
import sys
import socket
import MyTools
class Server(object):
def __init__(self, host, port, liscnt):
self.tcpSerSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.tcpSerSock.bind((host, port))
self.tcpSerSock.listen(liscnt)
self.tcpCliSock = []
self.namelist = []
self.locker = MyTools.MyLock()
self.running = True
MyTools.MyThread(self.__command, ()).start()
self.__mainloop()
self.tcpSerSock.close()
def __command(self):
try:
while raw_input().lower() != "quit":
pass
except (EOFError, KeyboardInterrupt):
pass
finally:
self.running = False
def __mainloop(self):
while self.running:
tcpCliSock, addr = self.tcpSerSock.accept()
self.locker.lock()
self.tcpCliSock.append(tcpCliSock)
self.locker.unlock()
MyTools.MyThread(self.__recv, (tcpCliSock,)).start()
def __recv(self, tcpCliSock):
BUFSIZ = 1024
name = ""
while self.running:
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
if not name:
name = data
data = " ".join((name, "\n+\n"))
self.__send(data, tcpCliSock, True)
self.namelist.append(name)
else:
data = " ".join((name, MyTools.clock(), "\n", data))
self.__send(data, tcpCliSock, False)
if name:
data = " ".join((name, "\n-\n"))
self.__send(data, tcpCliSock, False)
self.namelist.remove(name)
self.locker.lock()
self.tcpCliSock.remove(tcpCliSock)
self.locker.unlock()
tcpCliSock.close()
def __send(self, data, tcpCliSock, bFirst):
self.locker.lock()
if bFirst:
tcpCliSock.send(data + str(self.namelist))
for eachSock in self.tcpCliSock:
if eachSock is not tcpCliSock:
eachSock.send(data)
self.locker.unlock()
def main():
host = ""
port = 34567
liscnt = 5
argc = len(sys.argv)
if argc >= 2:
port = int(sys.argv[1])
if argc >= 3:
liscnt = int(sys.argv[2])
Server(host, port, liscnt)
if __name__ == "__main__":
main()
while self.running:
#~ data = tcpCliSock.recv(BUFSIZ)
try:
data = tcpCliSock.recv(BUFSIZ)
except:
break
#~ MyTools.MyThread(self.__recv, ()).start()
t = MyTools.MyThread(self.__recv, ())
t.daemon = True
t.start()
elif data[index:index+3] == "\n-\n":
#~ name = data[:index]
name = data[:index-1]
for i in range(self.listboxr.size()):
#~ if self.listboxr.see(i) == name:
if self.listboxr.get(i) == name:
self.listboxr.delete(i, i+1)
break
#!/usr/bin/python
"Client.py"
import socket
import Tkinter
import MyTools
import tkMessageBox
class Client(object):
def __init__(self):
self.__login()
def __login(self):
self.top = Tkinter.Tk()
self.top.title("login")
self.top.geometry("200x70")
text = (("host:", "localhost"), ("port:", "34567"), ("name:", ""))
for i in range(1, len(text)+1):
cmd = compile("""
frame%d = Tkinter.Frame(self.top)
label%d = Tkinter.Label(frame%d, text=%r)
label%d.pack(side=Tkinter.LEFT)
self.cwd%d = Tkinter.StringVar(self.top)
self.cwd%d.set(%r)
entry%d = Tkinter.Entry(frame%d, width=15, textvariable=self.cwd%d)
entry%d.pack(side=Tkinter.LEFT)
frame%d.pack()""" \
% (i, i, i, text[i-1][0], i, i, i, \
text[i-1][1], i, i, i, i, i), "", "exec")
exec cmd
framex = Tkinter.Frame(self.top)
button1 = Tkinter.Button(framex, text="login", command=self.__check)
button2 = Tkinter.Button(framex, text="close", command=self.__close)
button1.pack(side=Tkinter.LEFT)
button2.pack(side=Tkinter.LEFT)
framex.pack()
self.top.mainloop()
def __check(self, ev=None):
host = self.cwd1.get()
if not host:
tkMessageBox.showwarning("check", "host is invalid")
return
try:
port = int(self.cwd2.get())
except (ValueError, TypeError):
tkMessageBox.showwarning("check", "port is invalid")
return
self.name = self.cwd3.get()
if not self.name:
tkMessageBox.showwarning("check", "please write your name")
return
connected = False
try:
self.tcpCliSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.tcpCliSock.connect((host, port))
self.tcpCliSock.send(self.name)
connected = True
except:
self.tcpCliSock.close()
tkMessageBox.showerror("error", "can not connect to server")
self.__close()
del self.cwd1, self.cwd2, self.cwd3, self.top
if connected:
self.__start()
def __start(self, ev=None):
self.locker = MyTools.MyLock()
self.top = Tkinter.Tk()
self.top.title("Client")
self.topframe = Tkinter.Frame(self.top)
self.leftframe = Tkinter.Frame(self.topframe)
self.chatframe = Tkinter.Frame(self.leftframe)
self.scrollbarl = Tkinter.Scrollbar(self.chatframe)
self.scrollbarl.pack(side=Tkinter.RIGHT, fill=Tkinter.Y)
self.listboxl = Tkinter.Listbox(self.chatframe, height=15, width=50, \
yscrollcommand=self.scrollbarl.set)
self.scrollbarl.config(command=self.listboxl.yview)
self.listboxl.pack(side=Tkinter.LEFT, fill=Tkinter.BOTH)
self.chatframe.pack()
self.cwd = Tkinter.StringVar(self.leftframe)
self.entry = Tkinter.Entry(self.leftframe, width=50, \
textvariable=self.cwd)
self.entry.bind("<Return>", self.__send)
self.entry.pack()
self.buttonframe = Tkinter.Frame(self.leftframe)
self.button1 = Tkinter.Button(self.buttonframe, text="send", \
command=self.__send)
self.button2 = Tkinter.Button(self.buttonframe, text="clear", \
command=self.__clear)
self.button3 = Tkinter.Button(self.buttonframe, text="close", \
command=self.__close)
self.button1.pack(side=Tkinter.LEFT)
self.button2.pack(side=Tkinter.LEFT)
self.button3.pack(side=Tkinter.LEFT)
self.buttonframe.pack()
self.leftframe.pack(side=Tkinter.LEFT)
self.rightframe = Tkinter.Frame(self.topframe)
self.scrollbarr = Tkinter.Scrollbar(self.rightframe)
self.scrollbarr.pack(side=Tkinter.RIGHT, fill=Tkinter.Y)
self.listboxr = Tkinter.Listbox(self.rightframe, height=18, width=10, \
yscrollcommand=self.scrollbarr.set)
self.listboxr.insert(Tkinter.END, self.name)
self.scrollbarr.config(command=self.listboxr.yview)
self.listboxr.pack(side=Tkinter.LEFT, fill=Tkinter.BOTH)
self.rightframe.pack(side=Tkinter.LEFT)
self.topframe.pack()
MyTools.MyThread(self.__recv, ()).start()
self.top.mainloop()
def __clear(self):
self.locker.lock()
self.listboxl.delete(0, Tkinter.END)
self.locker.unlock()
def __close(self, ev=None):
self.top.quit()
self.top.destroy()
def __send(self, ev=None):
data = self.cwd.get()
self.cwd.set("")
if data.strip():
self.tcpCliSock.send(data)
info = " ".join((self.name, MyTools.clock()))
data = "".join((" " * 4, data))
self.locker.lock()
self.listboxl.insert(Tkinter.END, info)
self.listboxl.insert(Tkinter.END, data)
self.locker.unlock()
else:
tkMessageBox.showinfo("info", "can not send blank string")
def __recv(self):
BUFSIZ = 1024
while True:
data = self.tcpCliSock.recv(BUFSIZ)
if not data:
break
else:
index = data.find("\n")
if data[index:index+3] == "\n+\n":
names = data[index+3:]
if names:
namelist = eval(names)
for name in namelist:
self.listboxr.insert(Tkinter.END, name)
else:
name = data[:index-1]
self.listboxr.insert(Tkinter.END, name)
elif data[index:index+3] == "\n-\n":
name = data[:index]
for i in range(self.listboxr.size()):
if self.listboxr.see(i) == name:
self.listboxr.delete(i, i+1)
break
else:
info = data[:index].strip()
data = "".join((" " * 4, data[index+1:].strip()))
self.locker.lock()
self.listboxl.insert(Tkinter.END, info)
self.listboxl.insert(Tkinter.END, data)
self.locker.unlock()
self.tcpCliSock.close()
tkMessageBox.showerror("error", "disconnect to server")
def main():
Client()
if __name__ == "__main__":
main()