Python-chat (sample)

Source: Internet
Author: User

I wrote a very simple chat and will gradually add functions later. I mainly use wxpython + twisted, which mainly encountered a problem: mainloop of wxpython and reactor of twisted. run. You can solve this problem through reactor. registerWxApp (app ..

 

ChatClient. py

#-*-Coding: UTF-8-*-<br/> Import wx; <br/> Import wx. richtext as RT; <br/> Import sprint; <br/> from twisted. internet import wxreactor <br/> wxreactor. install () <br/> from twisted. internet import reactor, protocol <br/> class echoclient (protocol. protocol): <br/> "" chat client ""; <br/> F = ""; <br/> def connectionmade (Self ): <br/> Print "connection success"; <br/> def datareceived (self, data): <br/> PR INT "----- datareceived:" + data; <br/> self. F (data); <br/> def connectionlost (self, reason): <br/> "<br/> def send (self, S ): <br/> Print "Send:" + S <br/> self. transport. write (STR (S + '/R/N'); <br/> class echofactory (protocol. clientfactory): <br/> echocl = ''; <br/> clientsendfun =''; <br/> def startconnect (self, fun ): <br/> Print "----- startconnect" <br/> self. clientsendfun = fun; <br/> def B Uildprotocol (self, ADDR): <br/> Print "----- buildprotocol" <br/> self. echocl = echoclient (); <br/> self. echocl. factory = self; <br/> self. echocl. F = self. clientsendfun; <br/> return self. echocl; <br/> def sendtext (self, S): <br/> self. echocl. send (s); <br/> # def startedconnecting (self, connector): <br/> # print "connection success! "; <Br/> def clientconnectionfailed (self, connector, reason): <br/> Print" Connection Failed handle "; <br/> reactor. stop (); <br/> def clientconnectionlost (self, connector, reason): <br/> Print "Connection lost handle" <br/> reactor. stop () <br/> class myframe (wx. frame): <br/> def _ init _ (Self): <br/> wx. frame. _ init _ (self, none,-1, "chat", size = (500,400), style = wx. default_frame_style); <br/> self. alltext = ''; <Br/> self. loginname = ''; <br/> self. richtext = RT. richtextctrl (self,-1, style = wx. vscroll | wx. hscroll); <br/> self. richtext. seteditable (false); <br/> self. logintext = wx. textctrl (self,-1, size = (300,25) <br/> self. loginbtn = wx. button (self, wx. newid (), "login", size = (50, 30); <br/> self. loginbtn. BIND (wx. evt_button, self. login, self. loginbtn); <br/> self. sendtext = wx. textctrl (self,-1, size = (300,25)) <Br/> self. sendbtn = wx. button (self, wx. newid (), "send", size = (50, 30); <br/> self. sendbtn. BIND (wx. evt_button, self. sendtexthandle, self. sendbtn); <br/> topsizer = wx. boxsizer (wx. horizontal); <br/> topsizer. add (5, 5), 0) <br/> topsizer. add (self. logintext, 1, wx. expand | wx. all, 5); <br/> topsizer. add (5, 5), 0); <br/> topsizer. add (self. loginbtn); <br/> topsizer. add (5, 5), 0); <br/> bottomsizer = wx. boxsizer (W X. horizontal); <br/> bottomsizer. add (5, 5), 0) <br/> bottomsizer. add (self. sendtext, 1, wx. expand | wx. all, 5); <br/> bottomsizer. add (5, 5), 0); <br/> bottomsizer. add (self. sendbtn); <br/> bottomsizer. add (5, 5), 0); <br/> mainsizer = wx. boxsizer (wx. vertical); <br/> mainsizer. add (topsizer, 0, wx. expand | wx. all, 5); <br/> mainsizer. add (self. richtext, 1, wx. expand | wx. all, 5); <br/> mainsizer. add (5, 5), 0); <br/> mains Izer. add (bottomsizer, 0, wx. expand | wx. all, 5); <br/> self. setsizer (mainsizer); <br/> self. setfocus (); <br/> self. setenabled (true); <br/> def setenabled (self, B): <br/> self. richtext. enable (B) <br/> self. sendtext. enable (B) <br/> self. sendbtn. enable (B) <br/> def login (self, event): <br/> self. loginsuc (self. logintext. getvalue (); <br/> def loginsuc (self, name = ''): <br/> If (name! = ''): <Br/> self. loginname = self. logintext. getvalue (); <br/> self. fact = echofactory () <br/> self. fact. startconnect (self. gettext); <br/> reactor. connecttcp ("localhost", 1234, self. fact); <br/> def sendtexthandle (self, event): <br/> self. fact. sendtext ("<font color = '# cc0000'>" + self. loginname + '</font> say:' + self. sendtext. getvalue (); <br/> def gettext (self, S = ''): <br/> # self. alltext + = s; <br/> self. richtext. appendtext (s); <br/> def reflashtext (Self): <br/> self. richtext. setvalue (self. alltext); <br/> class MyApp (wx. APP): <br/> def oninit (Self): <br/> frame = myframe () <br/> frame. show (true) <br/> self. settopwindow (FRAME) <br/> return true; <br/> def demo (): <br/> APP = MyApp (0) <br/> reactor. registerwxapp (APP) <br/> # reactor. connecttcp ("localhost", 1234, echofactory (); <br/> reactor. run (0) <br/> If _ name _ = '_ main _': <br/> demo () <br/>

 

ChatServer. py

#-*-Coding: UTF-8-*-<br/> from twisted. internet import reactor; <br/> from twisted. internet import protocol; <br/> from twisted. protocols import basic; <br/> from twisted. internet. protocol import protocol, factory; <br/> from twisted. protocols. basic import linereceiver; <br/> from twisted. application import service, Internet; <br/> class chatserver (Basic. linereceiver): <br/> Print "server running ......" <Br/> def connectionmade (Self): <br/> Print "Get connection from:", self. transport. client; <br/> # self. message ("connection success! /N ") <br/> self. factory. clients. append (Self); <br/> # Reason: twisted. python. failure. failure; <br/> def connectionlost (self, reason): <br/> Print "Connection lost:", reason. geterrormessage (); <br/> self. factory. clients. remove (Self); <br/> def linereceived (self, line): <br/> Print "rec data:", line; <br/> for Cl in self. factory. clients: <br/> Cl. message (line); <br/> def message (self, message): <br/> self. transport. write (Message + '/R/N'); <br/> factory = protocol. serverfactory () <br/> factory. protocol = chatserver <br/> factory. clients = []; <br/> # application = service. application ("chatserver") <br/> # Internet. tcpserver (1234, factory ). setserviceparent (Application) <br/> reactor. listentcp (1234, factory) <br/> reactor. run () <br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.