This article comes from McDull's namesake blog
These days learned a little bit of Python network programming and Tkinter GUI interface programming, today roughly use, make up a simple duplex chat software, of course, the function is again simple, but can send and receive messages, show the message just, when playing, and now write a little bit of code, Basic can achieve receive messages and display received messages, of course, the client interface I did not do, or stay in the shell transceiver, server-side interface to do, but the interface is ugly, and can not display Chinese characters (to be improved), the server to the client did not do.
Of course, there are a lot of problems, at present, some I can not solve. Now in the program I just use two threads, are two infinite loops, one is socket, one is Tkinter mianloop.
First look at the current write a bit of server-side code it, quite chaotic, most of the results of the day into this. The widget should be text, which is somewhat inappropriate with a ListBox and entry.
#-*-coding:cp936-*-
From Tkinter Import *
From time import CTime
From socket Import *
Import Thread
Class 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 andRIGHT,padx= 20,pady=10)
#self. Sendmesgbutton.focus_set ()
Defsendmessageto (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
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 ()
Here is the UDP connection.
Client I still use the last one of the simplest.
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 ()
The operating result is:
Run server first.
650) this.width=650; "title=" a python program echnical 斺 exactly milk "alt=" a python program echnical 斺 exactly milk "src=" http://s3.sinaimg.cn/bmiddle/ 4b5039214721c1fc91a72&690 "/>
You can enter the message you want to send in entry, point send can be sent (currently the function is not completed, can only be displayed) For example, we enter the Displayyour message in the ListBox entry.
650) this.width=650; "title=" a python program echnical 斺 exactly milk "alt=" a python program echnical 斺 exactly milk "src=" http://s14.sinaimg.cn/bmiddle/ 4b5039214721c294b1c0d&690 "/>
Click Send.
650) this.width=650; "title=" a python program echnical 斺 exactly milk "alt=" a python program echnical 斺 exactly milk "src=" http://s5.sinaimg.cn/bmiddle/ 4b5039214721c2a7a1a94&690 "/>
The contents of entry are cleared and displayed in the listbox.
Then we run the client program. And at the prompt, enter the content "Can you receive my message?" carriage return.
650) this.width=650; "title=" a python program echnical 斺 exactly milk "alt=" a python program echnical 斺 exactly milk "src=" http://s14.sinaimg.cn/bmiddle/ 4b5039214721c3455e8fd&690 "/>
Then see the interface:
650) this.width=650; "title=" a python program echnical 斺 exactly milk "alt=" a python program echnical 斺 exactly milk "src=" http://s3.sinaimg.cn/bmiddle/ 4b5039214721c6a6485b2&690 "/>
The message has been received.
Just learned the socket and tkinter for two or three days, caught dead.
Next, I'll refine the other features when I'm free. My ideal is, must have the landing interface, login after the pop-up interface. You can set up communication ports, IP addresses, and so on. A few more convenient buttons ... Wait, I don't know if it's OK ...
This article is from the "lake and Laughter" blog, please make sure to keep this source http://hashlinux.blog.51cto.com/9647696/1794847
A python program--Chat