Python simple chat program example based on Socket, pythonsocket
This article describes the simple chat program implemented by Python Based on Socket. We will share this with you for your reference. The details are as follows:
Requirements:SCIENCE and MOOD are both software professionals. They get tired of popular chat software and want to develop a simple chat software to meet their personalized needs without losing their professional skills ", talk is easy, try to code it.
Technology:Socket. For details, refer to the previous article: Python Socket to implement simple TCP Server/client functions
Language:Python
Although sockets distinguish between servers and clients, the two are equal in chat programs, both of which are client programs.
Server. py
import sockethost = socket.gethostname()port = 12345s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)s.bind((host,port))s.listen(1)sock,addr = s.accept()print('Connection built')info = sock.recv(1024).decode()while info != 'exit': print('MOOD:'+info) send_mes = input() sock.send(send_mes.encode()) if send_mes =='exit': break info = sock.recv(1024).decode()sock.close()s.close()
Client. py
import sockets= socket.socket()host = socket.gethostname()port = 12345s.connect((host,port))print('Linked')info = ''while info != 'exit': print('SCIENCE:'+info) send_mes=input() s.send(send_mes.encode()) if send_mes =='exit': break info = s.recv(1024).decode()s.close()
Effect:
Well, it's too low? No graphical interface. Don't talk about other personalized features. Hope they continue to work hard and make their own chat software as soon as possible!