一個python程序——聊天

本文來自麥兜響噹噹的同名博文html

這幾天學了python的一點網絡編程和Tkinter的GUI界面編程,今天大致用一下,編一個簡單的雙工的聊天軟件,固然功能是再簡單不過了,只是能 收發消息,顯示消息而已,就當玩玩了,目前寫了一點點代碼,基本能實現收消息和顯示收到的消息了,固然客戶端的界面我也沒有作,仍是停留在Shell上收 發,服務器端界面作了,不過界面作的很醜,並且不能顯示漢字(有待改進),服務器向客戶端發也沒作。python

  

   固然,這裏面有不少問題的,目前來講有些我還解決不了。如今的程序中我只是用到了兩個線程,都是兩個無限循環,一個是socket的收,一個是Tkinter的mianloop。編程

   先看一下目前寫的一點server端得代碼吧,挺亂的,多半天的成果就成這樣啦。widget應該是Text的,這裏用了Listbox和Entry有些不妥。服務器

 # -*- coding: cp936 -*-網絡

from Tkinter import *
from time import ctime
from socket import *
import threadsocket

class ChatInterFace(object):
   def __init__(self,initinfo=None):
       self.top=Tk()
       self.top.title('PPChat server v1.0')
       self.top.geometry('450x550')ide

       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()oop

                                                
   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)url

            self.MessageOut.insert(END,'')
           self.MessageIn.delete(0,message.__len__())
       else:
           pass

def 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 ctime

HOST='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 data

ChatCliSock.close()

 

    運行結果是:

    先運行server。

    一個python程序鈥斺斄奶

    能夠本身在Entry中輸入要發的消息,點send能夠發送(目前發生函數沒有完成,只能顯示)好比咱們在Entry中輸入Display your message in the listbox。

    一個python程序鈥斺斄奶

   點send。

   一個python程序鈥斺斄奶

   隨即Entry中的內容清除,而在listbox中顯示。

   而後咱們運行客戶端程序。並在提示符下輸入內容「can you receive my message?" 回車。

   一個python程序鈥斺斄奶

   而後看到界面:

   一個python程序鈥斺斄奶

   消息已經收到。

   

   剛學了兩三天的socket和Tkinter,獻醜了。

   接下來,有空的時候我會完善其餘功能的。個人理想是,必須有登錄界面,登錄後彈出收發界面。能夠設置通訊端口,IP地址等。方便的按鈕多幾個。。。。。。。。等等吧,不知道行不行。。。。。

相關文章
相關標籤/搜索