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.