Python develops a simple chat tool

Source: Internet
Author: User
Python is so powerful that it can do anything, haha, a joke. But what we're going to talk about today is really a very magical application.

Use Python to write a chat tool

In fact, we usually use the QQ similar chat tool, is also used to chat with the socket, but it also contains more complex features. The rationale is the same.

Python realizes the chat function, mainly uses the socket module. Let's go directly to the example below.


Server Side

Import Sockets=socket.socket () #建立socket链接s. Bind ((' 127.0.0.1 ', 8000)) #监听连接请求, of which 1 is the listener for a s.listen (1) #进行循环, Listen to the message from the client while 1: #获取链接IP和端口    conn,addr=s.accept ()    print ' [' +addr[0]+ ': ' +str (addr[1]) + '] Send a message To me: ' +conn.recv (1024x768)    Conn.sendall (' I received a message from [' +addr[0]+ ': ' +str (addr[1]) + '] ') s.close ()

Client side, relatively simple do not need to listen, do not need to loop

Import Sockets=socket.socket () #链接s. Connect ((' 127.0.0.1 ', 8000)) #获取键盘输入msg = raw_input ("Please input your message:") S.sendall (msg) print s.recv (1024x768) s.close ()

Very simple, haha, you can build on this code to optimize the creation of more powerful features

  • Related Article

    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.