• 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 客户端程序运行结果

    客户端运行结果

    OPTIMISM, PASSION & HARDWORK
  • 相关阅读:
    PhotoShop使用指南(3)—— 将多张图片添加到图层
    PhotoShop使用指南(2)——下雨动画效果
    PhotoShop使用指南(1)——动态图gif的制作
    SQL使用指南(2)—— 约束的使用
    SQL使用指南(1)—— 数据定义语言(DDL)
    pyspider显形js报错处理
    123123
    mongodb数据库操作,导入导出,增删改查
    selenium+phantomjs动态添加headers信息,动态加载
    使用selenium模拟登陆点击登陆按钮
  • 原文地址:https://www.cnblogs.com/hiramlee0534/p/6049468.html
Copyright © 2020-2023  润新知