Python simple chat program example based on Socket, pythonsocket

Source: Internet
Author: User

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!

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.