Home >Backend Development >Python Tutorial >Detailed introduction to Python+Socket to implement Chinese automatic reply chat function between client and server based on TCP protocol

Detailed introduction to Python+Socket to implement Chinese automatic reply chat function between client and server based on TCP protocol

巴扎黑
巴扎黑Original
2017-09-02 13:10:511712browse

This article mainly introduces Python+Socket to implement the Chinese automatic reply chat function between the client and the server based on the TCP protocol. It analyzes the related operation methods and precautions of Python+Socket to implement the TCP chat program with automatic reply function in the form of examples. , Friends in need can refer to the following

This article describes the example of Python+Socket implementing the Chinese automatic reply chat function between the client and the server based on the TCP protocol. I share it with you for your reference. The details are as follows:

[Tucao]

The code on the Internet kills people. It seems that the writing is conclusive. If it can be run, it will work. question.
Some friends who love codes and like to collect codes will paste and copy other people's codes when they see them. But at least you can try running it, brother

[Text]

Yesterday I modified the C/S chat program to run the UDP protocol, but the TCP protocol didn’t work. no. Various trials, various pitfalls.

After making the following modifications, it finally works:

1. Encode and decode the sent and received information respectively
2, The 10th step of the client Change the line bind to connect (This is really a big pit!!)

(This article is based on windows 7 + python 3.4)

The complete code is as follows (head guarantee , I personally tested it and it’s normal!):

Server:


##

# tcp_server.py
'''服务器'''
from socket import *
from time import ctime
HOST = '' #主机地址
PORT = 23345 #端口号
BUFSIZ = 2048 #缓存区大小,单位是字节,这里设定了2K的缓冲区
ADDR = (HOST, PORT) #链接地址
tcpSerSock = socket(AF_INET, SOCK_STREAM) #创建一个TCP套接字
tcpSerSock.bind(ADDR) #绑定地址
tcpSerSock.listen(5) #最大连接数为5
while True: #无限循环
  print('尝试连接客户端。。。')
  tcpCliSock, addr = tcpSerSock.accept() #等待接受连接
  print('链接成功,客户端地址为:', addr)
  while True:
    data = tcpCliSock.recv(BUFSIZ) #接收数据,BUFSIZ是缓存区大小
    if not data: break #如果data为空,则跳出循环
    print(data.decode())
    msg = '{} 服务器已接收 [自动回复]'.format(ctime())
    tcpCliSock.send(msg.encode())
  tcpCliSock.close() #关闭连接
tcpSerSock.close() #关闭服务器

Client:

##

# tcp_client.py
'''客户端'''
from socket import *
from time import ctime
HOST = 'localhost' #主机地址
PORT = 23345 #端口号
BUFSIZ = 2048 #缓存区大小,单位是字节,这里设定了2K的缓冲区
ADDR = (HOST, PORT) #链接地址
tcpCliSock = socket(AF_INET, SOCK_STREAM) #创建一个TCP套接字
#tcpCliSock.bind(ADDR) #绑定地址
tcpCliSock.connect(ADDR) #绑定地址
while True:
  msg = input('请输入:') #输入数据
  if not msg: break #如果 msg 为空,则跳出循环
  tcpCliSock.send(msg.encode())
  data = tcpCliSock.recv(BUFSIZ) #接收数据,BUFSIZ是缓存区大小
  if not data: break #如果data为空,则跳出循环
  print(data.decode())

[Run Screenshot]

Experimental method: Run the server first, then run the client

Then you can run the client The client can freely chat with the server:

The above is the detailed content of Detailed introduction to Python+Socket to implement Chinese automatic reply chat function between client and server based on TCP protocol. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn