• python-fork聊天室


      1 服务端
      2  #!/usr/bin/python
      3 
      4 from socket import *
      5 import sys
      6 import os
      7 
      8 class Node(object):
      9     def __init__(self,val,next = None):
     10         self.val = val
     11         self.next = next
     12 
     13 def do_login(s,H,msg,clientaddr):
     14     msg = msg.split(' ')
     15     msg[2] = '%s login....'%msg[1]
     16     
     17     p  = Node(clientaddr)
     18     q = H.next
     19 
     20     while q != None:
     21         s.sendto(msg[2],q.val)
     22         q = q.next
     23 
     24     p.next = H.next
     25     H.next = p
     26     
     27     return
     28 
     29 def do_quit(s,H,msg,clientaddr):
     30     msg = "%s log out..."%msg.split(' ')[1]
     31 
     32     p = H
     33 
     34     while p.next != None:
     35         if p.next.val != clientaddr:
     36             s.sendto(msg,p.next.val)
     37             p = p.next
     38 
     39         else:
     40             q = p.next
     41             p.next = q.next
     42 
     43     return
     44     
     45 
     46 def do_chat(s,H,msg,clientaddr):
     47     p = H.next
     48     buf = "%s say %s"%(msg.split(' ')[1],msg.split(' ')[2])
     49 
     50     while p != None:
     51         if p.val != clientaddr:
     52             s.sendto(buf,p.val)
     53         p = p.next
     54 
     55     return
     56 
     57 def do_parent(s,addr):
     58     #msg = type + name + text
     59     msg = "B server "
     60 
     61     while True:
     62         print "system message >>"
     63         text = sys.stdin.readline()
     64         msg = msg + text
     65         s.sendto(msg,addr)
     66     s.close()
     67 
     68 def do_child(s):
     69     H = Node(None)
     70 
     71     while True:
     72         msg,clientaddr = s.recvfrom(4096)
     73         tmp = msg.split(' ')
     74 
     75         if tmp[0] == 'L':
     76             do_login(s,H,msg,clientaddr)
     77         if tmp[0] == 'B':
     78             do_chat(s,H,msg,clientaddr)
     79         if tmp[0] == 'Q':
     80             do_quit(s,H,msg,clientaddr)
     81 
     82     return
     83 
     84 def main():
     85     HOST = sys.argv[1]
     86     PORT = int(sys.argv[2])
     87     ADDR = (HOST,PORT)
     88 
     89     s = socket(AF_INET,SOCK_DGRAM,0)
     90     s.bind(ADDR)
     91 
     92     pid = os.fork()
     93 
     94     if pid < 0:
     95         print "fail to create process"
     96         return
     97     elif pid == 0:
     98         do_child(s)
     99     else:
    100         do_parent(s,ADDR)
    101 
    102 
    103 if __name__ == "__main__":
    104     main()
     1 #客户端
     2 #!/usr/bin/python
     3 
     4 from socket import *
     5 import sys
     6 from signal import *
     7 import os
     8 
     9 
    10 def do_child(s,addr,msg):
    11     name = msg.split(' ')[1]
    12     while True:
    13         text = raw_input(">>")
    14 
    15         if text == 'quit':
    16             msg = 'Q ' + name
    17             s.sendto(msg,addr)
    18             os.kill(os.getppid(),SIGKILL)
    19             exit()
    20         else:
    21             msg = 'B %s %s'%(name,text)
    22             s.sendto(msg,addr)
    23     return
    24 
    25 def do_parent(s):
    26     while True:
    27         msg,addr = s.recvfrom(2048)
    28         print msg
    29 
    30 def main():
    31     HOST = sys.argv[1]
    32     PORT = int(sys.argv[2])
    33     ADDR = (HOST,PORT)
    34     
    35     s = socket(AF_INET,SOCK_DGRAM,0)
    36 
    37     name = raw_input("please input your name>>")
    38 
    39     msg = 'L %s '%name
    40 
    41     s.sendto(msg,ADDR)
    42 
    43     pid = os.fork()
    44 
    45     if pid < 0:
    46         print "fail to create process"
    47         return
    48     elif pid == 0:
    49         do_child(s,ADDR,msg)
    50     else:
    51         do_parent(s)
    52         
    53 
    54 if __name__ == "__main__":
    55     main()
  • 相关阅读:
    通过Nginx,Tomcat访问日志(access log)记录请求耗时
    Nginx+Lua+Redis 对请求进行限制
    windows7+eclipse+hadoop2.5.2环境配置
    ubuntu + hadoop2.5.2分布式环境配置
    CentOS6.5上golang环境配置
    curl POST
    .sh 的运行
    CentOS 安装nginx
    Amazon ec2 改成密码登录方式
    SSH 服务器不用密码
  • 原文地址:https://www.cnblogs.com/chengyunshen/p/7196367.html
Copyright © 2020-2023  润新知