这几天学了python的一点网络编程和Tkinter的GUI界面编程,今天大体用一下,编一个简单的双工的聊天软件,当然功能是再简单不过了,只是能 收发消息,显示消息而已,就当玩玩了,目前写了一点点代码,基本能实现收消息和显示收到的消息了,当然客户端的界面我也没有做,还是停留在Shell上收 发,服务器端界面做了,不过界面做的很丑,而且不能显示汉字(有待改进),服务器向客户端发也没做。

  

   当然,这里面有很多问题的,目前来说有些我还解决不了。现在的程序中我只是用到了两个线程,都是两个无限循环,一个是socket的收,一个是Tkinter的mianloop。

   先看一下目前写的一点server端得代码吧,挺乱的,多半天的成果就成这样啦。widget应该是Text的,这里用了Listbox和Entry有些不妥。

# -*- coding: cp936 -*-
from Tkinter import *
from time import ctime
from socket import *
import threadclass ChatInterFace(object):
   def __init__(self,initinfo=None):
       self.top=Tk()
       self.top.title('PPChat server v1.0')
       self.top.geometry('450x550')       self.MessageOut=Listbox(self.top,fg='red')
       self.MessageOut.pack(expand=1,fill=BOTH)
  
       
       
       self.MessageIn=Entry(self.top,fg='red')
       self.MessageIn.pack(padx=20,pady=50,fill=BOTH)       #发送消息按钮
       self.sendMesgButton=Button(self.top,text='send',width=10,command=self.SendMessageTo)
       #self.sendMesgButton.bind("<Return>",self.SendMessageTo)
       self.sendMesgButton.pack(side=BOTTOM and RIGHT,padx=20,pady=10)
       #self.sendMesgButton.focus_set()                                                
   def SendMessageTo(self):        #let the message from the Entry displayed in Listbox
       message=self.MessageIn.get()
       if message:
           self.MessageOut.insert(END,'you said [%s]:'% ctime())
           self.MessageOut.insert(END,message)            self.MessageOut.insert(END,'')
           self.MessageIn.delete(0,message.__len__())
       else:
           passdef main():
   d=ChatInterFace()
   HOST='localhost'
   PORT=71628
   BUFSIZ=1024
   ADDR=(HOST,PORT)   ChatSerSock=socket(AF_INET,SOCK_DGRAM)
   ChatSerSock.bind(ADDR)
   def socketproc():
       while True:
           data,addr=ChatSerSock.recvfrom(BUFSIZ)
           #print 'received from %s : [%s]:%s' % (addr[0],ctime(),data)
           d.MessageOut.insert(END,'your friend said [%s]:' % ctime())
           d.MessageOut.insert(END,data)            d.MessageOut.insert(END,'')
           
     
       ChatSerSock.close()
   thread.start_new_thread(socketproc,())
   thread.start_new_thread(mainloop,())       if __name__=='__main__':
   main()

 

   这里用的UDP的连接。

   客户端我还是用的上次那个最简单的。

from socket import *
from time import ctimeHOST='localhost'
PORT=71628
BUFSIZ=1024
ADDR=(HOST,PORT)ChatCliSock=socket(AF_INET,SOCK_DGRAM)
while True:
   data = raw_input('>')
   if not data:
       break
   ChatCliSock.sendto(data,ADDR)
   data,ADDR = ChatCliSock.recvfrom(BUFSIZ)
   if not data:
       break
   print dataChatCliSock.close()

    运行结果是:

    先运行server。

    

python 聊天 主题 python 做聊天程序_shell

    可以自己在Entry中输入要发的消息,点send可以发送(目前发生函数没有完成,只能显示)比如我们在Entry中输入Display your message in the listbox。

    

python 聊天 主题 python 做聊天程序_shell_02

   点send。

   

python 聊天 主题 python 做聊天程序_python_03

   随即Entry中的内容清除,而在listbox中显示。

   然后我们运行客户端程序。并在提示符下输入内容“can you receive my message?" 回车。

   

python 聊天 主题 python 做聊天程序_客户端_04

   然后看到界面:

   

python 聊天 主题 python 做聊天程序_网络_05

   消息已经收到。

   

   刚学了两三天的socket和Tkinter,献丑了。

   接下来,有空的时候我会完善其他功能的。我的理想是,必须有登陆界面,登陆后弹出收发界面。可以设置通信端口,IP地址等。方便的按钮多几个。。。。。。。。等等吧,不知道行不行。。。。。



转载于:https://blog.51cto.com/hashlinux/1794847