python局域网聊天_【Python】iichats —— 命令行下的局域网聊天程序

1 #!/usr/bin/python

2 #coding:utf8

3 #python 2.7.6

4

5 importthreading6 importsocket7 importtime8 importos9 importsys10 importsignal11 from readline importget_line_buffer12 BUFSIZE = 1024

13 BACKPORT = 7789 #状态监听端口

14 CHATPORT = 7788 #聊天信息发送窗口

15 START = '>>'

16 INIT = '>>'

17 users ={}18 ips ={}19 #起到双向字典的作用,ip和name互相映射

20

21 #数据处理类(消息封装、分解)

22 classData():23 defgettime(self):24 return time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time()))25 defgetip(self):26 ip = os.popen("/sbin/ifconfig | grep 'inet addr' | awk '{print $2}'").read()27 ip = ip[ip.find(':')+1:ip.find('\n')]28 returnip29 defhandlebc(self, data):30 data = data[5:]31 res = data.split('#opt:')32 returnres33 defmakebc(self, name, switch):34 data = 'name:%s#opt:%d' %(name, switch)35 returndata36 defhandlechat(self, data):37 msg = '\n' + self.gettime() + '\n' +'from'+ data + '\n'

38 returnmsg39 defmakechat(self, data, name):40 return name + ':' +data41

42 #后台监听类

43 classBack(threading.Thread):44 def __init__(self):45 threading.Thread.__init__(self)46 self.data =Data()47 self.addrb = ('255.255.255.255', BACKPORT)48 self.addrl = ('', BACKPORT)49 self.name =socket.gethostname()50 self.ip =self.data.getip()51 self.thread_stop =False52 defstatus(

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值