Python full-duplex socket chat

Source: Internet
Author: User


Self-learning python for some time, always want to get an interesting thing, so take the socket to do a chat room, can one-to-many, single-to full-duplex chat. The following may be perfect code in play anything a GUI interface, compared with the Force lattice 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0061.gif "alt=" j_0061.gif "/ >.


Service side:

Using the Socketserver module, multithreading asynchronously processes client messages, accepts customer messages, and forwards both service-side as a broker.

Added login to sign up for multiplayer chat one-off function

Client:

The main thread connects to the server, and two sub-threads are responsible for both reading and writing


Server

Socketservertime ctimethreadingtracebackqueuedb dblock=threading. Lock () local_school = threading.local () Handler ():    queue = []     db = db ()     user_name = {}     ( Sock):        .sock = sock         .input = [.sock]         ():         DATA = .SOCK.RECV (). Strip ()          data     (data):         .sock.sendall ( Data)      ():         .send ()          .sock.close ()         .queue.remove (. Sock )         .user_name[local_school.user]     ():         .sock.close ()         .queue.remove (. Sock)          .user_name[local_school.user]     (UserData):         sock .queue:             Sock.sendall (%  (CTime () userdata))      (User_sockuserdata):         .user_name[user_sock].sendall (%  (CTime () userdata))      ( UserData):         time_data = ctime ()          sock [x x .queue x != .sock]:             sock.sendall (%  (time_datauserdata))       ():          funcdict = {            :  .login: .registered}        :             .sock.send ()              DATA = .RECV ()              data == :                 .stop ()                  data funcdict:                 funcdict[data] ()             :                 .handler ()       &nBsp;  :            .exit ()       ():         .send ()          DATA = .RECV ()         data == :             .send ()              DATA = .RECV ()              data == :                 .stop ()                  data == :                 .handler ()              :   &nBsp;            .login ()          user_data = data.split ()          ( User_data)  == :            user =  user_data[]            passwd =  user_data[]            user_data = . Db.get_data ()  {}            user user_ data user_data[user] == passwd:                 local_school.user=user                 .send ()                &nbsP; .queue.append (. Sock)                  .broadcast (% user)                  .user_name[user]=.sock  .send ()                  DATA=.RECV ()                  data==:                     . Ltian ()                 data==:                      .one_to_one ()                  :       &nBsp;            .send ()                      .handler ()             :                 .send ()                  .login ()          :             .send ()              .login ()      ():         .send ()         data = .recv ()          data == :             .send ()             data = .recv ()              data == :                 .stop ()                  .send ()              :                 .handler ()         user_data = data.split ()          (User_data)  == :             user = user_data[]             passwd = user_data[]            &nbSp;db_data = .db.get_data ()  {}             user db_data:                 .send ()                  .registered ()             :                 db_data[user] =  Passwd                local_ school.user=user                 lock.acquire ()                  :                     .db.put_data (Db_data)                  :                     lock.release ()                  .broadcast (% user)                  .queue.append (. Sock)                  .user_name[user] = .sock                 . Ltian ()                 .queue         :             .send ()          &NBsp;  .registered ()      ():  .queue         .send ()         :             DATA = .RECV ()              data==:                 % .queue                 .stop ()% .queue                 .yiduiduo (Local_school.userdata)    ():         .send ()         user_data=.recv () [:]         user_data==local_school.user:             .one_to_one ()         user_data .db.get_data ():             .user_name.get (User_data)  .user_name[user_data] . Queue:                .send ()                 :                      DATA=.RECV ()                      data==:                         .send ()                           DATA=.RECV ()                          data==:                             .one (User_datalocal_ School.user)                              .stop ()                               data==:                             . Ltian ()                      data==.user_name. Get (User_data):. One (User_datalocal_school.userdata)              :                 .send ()                 . One_to_one ()         :             .send ()             .one_to _one () MyServer (Socketserver.baserequesthandler):     ():         .client_address        .mysock = handler (. Request)         .mysock.queue         .mysock.handler () __name__ == :    host = port =  addr =  (Hostport)     server = socketserver.threadingtcpserver (Addrmyserver)      server.request_queue_size=server.serve_forever ()


Client


Socket *threadingthreads=[]client_handler ():     (ipadr=port=):         .sock = socket (Af_inetsock_stream)          .sock.connect ((ipadrport))         .input=[.sock]         .input     (data):         .sock.sendall (data)      (:       )  DATA = .SOCK.RECV (). Strip ()         data         data     ():         :            :                 data= ()                  data==:                     .send ()                      .sock.close ()                      .send (data)              :                  ():         :             :                 .RECV ()              :                 a1=clIent_handler () chat = threading. Thread (=a1.write)  threads.append (chat) chat = threading. Thread (=a1.read)   threads.append (chat) threadsi  ((Threads)):     threads[i]. Start ()


This article is from the "13275081" blog, please be sure to keep this source http://13285081.blog.51cto.com/13275081/1963737

Python full-duplex socket chat

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.