Python implements chat applets and python chat applets

Source: Internet
Author: User

Python implements chat applets and python chat applets

The example in this article shares with you the code for implementing the chat applet in python for your reference. The details are as follows:

What I implement here is the function of communication between the client and the server, which is relatively simple and not the same as the group chat in the previous article.

Server. py

#-*-Coding: UTF-8-*-import socket, traceback, syshost = ''port = 51423 s = socket. socket (socket. AF_INET, socket. SOCK_STREAM) s. setsockopt (socket. SOL_SOCKET, socket. SO_REUSEADDR, 1) s. bind (host, port) s. listen (1) ClientSock, ClientAddr = s. accept () while 1: try: buf = ClientSock. recv (1024) if len (buf): print "the client says:" + buf data = raw_input ("the server says:") ClientSock. sendall (data) protocol T: print "Dialogue Over" ClientSock. close () sys. exit (0)

Client. py

#-*-Coding: UTF-8-*-import socket, syshost = '2017. 168.80.21 '# host = raw_input ("Plz imput destination IP:") # data = raw_input ("Plz imput what you want to submit:") port = 51423 s = socket. socket (socket. AF_INET, socket. SOCK_STREAM) try: s. connect (host, port) failed t socket. gaierror, e: print "Address-related error connecting to server: % s" % e sys. exit (1) using t socket. error, e: print "Connection error: % s" % e sys. exit (1) while 1: try: data = raw_input ("the client says:") s. send (data) buf = s. recv (1024) if len (buf): print "server:" + buf handle T: print "Dialogue Over" s. close () sys. exit (0)

Result Display

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.