服务端代码:
import socket
import time
import tkinter
from tkinter.scrolledtext import ScrolledText
import threading
import requests
import tkinter
from socket import *
from time import ctime
from tkinter import *
global Text_Show # 调用人工智能对话的函数,返回人工智能回答
def AI_Talk(s):
print(s)
response = requests.post("http://www.tuling123.com/openapi/api", data={
"key": "4105cb6db8c54f5db470b6c0ee42d24b",
"info": s})
response = response.json()
answer = response['text']
return answer
# 线程二函数,用来进行对话
def Sever_Thread(sock, caddr):
Text_Show.insert('end', "客户端@" + str(caddr[1]) + "已连接!\n")
while True:
# 接收数据
data = str(sock.recv(1024).decode('UTF-8'))
if data == "quit":
Text_Show.insert('end', "客户端@" + str(caddr[1]) + "终止了对话\n")
Text_Show.see(tkinter.END)
break
else:
Text_Show.insert('end', "来自客户端@" + str(caddr[1]) + "的消息为:" + data + '\n')
Text_Show.see(tkinter.END)
# 发送数据
time.sleep(0.2)
data = AI_Talk(data) #
# data = '请输入发送至客户端的数据: 123123' #如果要手动输入的话就要设置好线程sleep时间不然还没有输入,就已经到其他线程了,就会发不出去。
sock.sendall(bytes(data, 'UTF-8'))
sock.close() # 线程一函数,监听端口,一旦有客户端接入,开启线程二
def Sever_Accept(ss):
while True:
sock, caddr = ss.accept()
Thread2 = threading.Thread(target=Sever_Thread, args=(sock, caddr))
Thread2.daemon = True # 线程守护
Thread2.start()
# 服务器初始化
def Sever_Init():
HOST = ''
PORT = 4700
ADDR = (HOST, PORT)
ss = socket(AF_INET, SOCK_STREAM, 0)
ss.bind(ADDR)
ss.listen(20) # 允许最大监听数
Thread1 = threading.Thread(target=Sever_Accept, args=(ss,))
Thread1.daemon = True # 线程守护
Thread1.start()
# 主函数
if __name__ == "__main__":
root = tkinter.Tk()
root.title("聊天小程序服务器端 ")
frame1 = Frame(root)
frame1.pack()
IP_Show_Label = Label(frame1, text="默认IP:127.0.0.1\n默认端口为6000\n无法更改!!!")
IP_Show_Label.pack(side='left')
frame2 = Frame(root)
frame2.pack()
Text_Show = ScrolledText(frame2, width=100, height=30)
Text_Show.bind("", lambda e: "break")
Text_Show.pack(side="bottom", fill='both', expand=True)
Sever_Init()
root.mainloop()
客户端代码:
import socket
import time
import tkinter
from tkinter.scrolledtext import ScrolledText
import threading
import requests
import tkinter
from socket import *
from time import ctime
from tkinter import *
global cs, Text_Show, Send_Show
# 发送按键的函数
def Click_Send():
sendData = Send_Show.get() # 获取输入内容
if sendData == 'quit':
Text_Show.insert(tkinter.END, "与服务器连接已断开" + "\n")
Text_Show.see(tkinter.END)
cs.sendall(bytes(sendData, encoding="utf8"))
cs.close()
else:
Text_Show.insert(tkinter.END, "客户端:" + sendData + "\n")
Text_Show.see(tkinter.END)
cs.sendall(bytes(sendData, encoding="utf8"))
Send_Show.delete(0, END)
# 线程函数,循环接受客户端消息
def Receive_Data():
while True:
accept_data = str(cs.recv(1024), encoding="utf8")
Text_Show.insert(tkinter.END, "服务器:" + accept_data + "\n")
Text_Show.see(tkinter.END)
# 主函数
if __name__ == "__main__":
# 初始化GUI
root = tkinter.Tk()
root.title("聊天小程序客户端 ")
# 顶部显示部分
frame1 = Frame(root)
frame1.pack()
IP_Show_Label = Label(frame1, text="本程序默认IP:127.0.0.1\n默认端口为6000\n无法更改!!!")
IP_Show_Label.pack(side='left')
# 中部聊天框显示部分
frame2 = Frame(root)
frame2.pack()
Text_Show = ScrolledText(frame2, width=70, height=15)
Text_Show.bind("", lambda e: "break")
Text_Show.pack(side="bottom", fill='both', expand=True)
# 底部消息发送部分
frame3 = Frame(root)
frame3.pack()
e3 = StringVar()
Send_Show = Entry(frame3, textvariable=e3, width=60)
buttontext2 = tkinter.StringVar()
buttontext2.set('发送')
button_Send = tkinter.Button(frame3, width=10, textvariable=buttontext2, command=Click_Send)
Send_Show.pack(side="left")
button_Send.pack(side="left")
frame3.pack()
# 初始化TCP协议
HOST = '127.0.0.1'
PORT = 4700
BUFSIZ = 1024
ADDR = (HOST, PORT)
cs = socket(AF_INET, SOCK_STREAM, 0)
cs.connect(ADDR)
thread = threading.Thread(target=Receive_Data)
thread.start()
root.mainloop()
运行效果: