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

12345678910111213 import sockets=socket.socket()#建立socket链接s.bind((‘127.0.0.1‘,8000))#监听连接请求,其中的1 ,是指监听一个s.listen(1)#进行循环,一直监听client发来的消息while 1:#获取链接IP和端口    conn,addr=s.accept()    print ‘[‘+addr[0]+‘:‘+str(addr[1])+‘] send a message to me: ‘+conn.recv(1024)    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

123456789 import sockets=socket.socket()#链接s.connect((‘127.0.0.1‘8000))#获取键盘输入msg = raw_input("Please input your message:")s.sendall(msg)print s.recv(1024)s.close()

Python develops a simple chat tool

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.