码迷,mamicode.com
首页 > 编程语言 > 详细

python 聊天室

时间:2016-11-10 07:38:43      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:use   count()   pytho   log   lob   net   rom   rmi   python   

server端程序

 1 # -*- coding: utf-8 -*-
 2 #!/usr/bin/python 
 3 """
 4 
 5 """
 6 import socket
 7 import sys
 8 import threading
 9  
10 con = threading.Condition()
11 HOST = raw_input("input the server‘s ip adrress: ") # Symbolic name meaning all available interfaces
12 PORT = 8888 # Arbitrary non-privileged port
13 data = ‘‘
14  
15 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
16 print Socket created
17 s.bind((HOST, PORT))
18 s.listen(10)
19 print Socket now listening
20  
21 #Function for handling connections. This will be used to create threads
22 def clientThreadIn(conn, nick):
23     global data
24 #infinite loop so that function do not terminate and thread do not end.
25     while True:
26     #Receiving from client
27         try:
28             temp = conn.recv(1024)
29             if not temp:
30                 conn.close()
31                 return
32             NotifyAll(temp)
33             print data
34         except:
35             NotifyAll(nick + " leaves the room!")
36             print data
37             return
38  
39     #came out of loop
40  
41 def NotifyAll(sss):
42     global data
43     if con.acquire():
44         data = sss
45         con.notifyAll()
46         con.release()
47   
48 def ClientThreadOut(conn, nick):
49     global data
50     while True:
51         if con.acquire():
52             con.wait()
53             if data:
54                 try:
55                     conn.send(data)
56                     con.release()
57                 except:
58                     con.release()
59                     return
60                      
61  
62 while 1:
63     #wait to accept a connection - blocking call
64     conn, addr = s.accept()
65     print Connected with  + addr[0] + : + str(addr[1])
66     nick = conn.recv(1024)
67      #send only takes string
68     #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
69     NotifyAll(Welcome  + nick +  to the room!)
70     print data
71     print str((threading.activeCount() + 1) / 2) +  person(s)!
72     conn.send(data)
73     threading.Thread(target = clientThreadIn , args = (conn, nick)).start()
74     threading.Thread(target = ClientThreadOut , args = (conn, nick)).start()
75  
76 s.close()

client端程序

 1 # -*- coding: utf-8 -*-
 2 #/usr/bin/python
 3 
 4 """
 5 
 6 """
 7  
 8 import socket
 9 import threading
10  
11  
12 inString = ‘‘
13 outString = ‘‘
14 nick = ‘‘
15  
16 def DealOut(s):
17     global nick, outString
18     while True:
19         outString = raw_input()
20         outString = nick + :  + outString
21         s.send(outString)
22  
23 def DealIn(s):
24     global inString
25     while True:
26         try:
27             inString = s.recv(1024)
28             if not inString:
29                 break
30             if outString != inString:
31                 print inString
32         except:
33             break
34          
35  
36 nick = raw_input("input your nickname: ")
37 ip = raw_input("input the server‘s ip adrress: ")
38 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
39 sock.connect((ip, 8888))
40 sock.send(nick)
41  
42 thin = threading.Thread(target = DealIn, args = (sock,))
43 thin.start()
44 thout = threading.Thread(target = DealOut, args = (sock,))
45 thout.start()

运行结果

1 在服务器端运行程序

技术分享

2 客户端程序运行结果

技术分享

客户端运行结果

技术分享

python 聊天室

标签:use   count()   pytho   log   lob   net   rom   rmi   python   

原文地址:http://www.cnblogs.com/hiramlee0534/p/6049468.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!