python3.0环境下利用tkinter模块的可视化编写的基于TCP的可视化社交平台(服务端)的实现
服务端源代码:
# coding:utf-8
"""
Version 1.1.0
Author lkk
Email [email protected]
DESC 基于TCP的界面社交平台(服务端)的实现
"""
import tkinter
import socket
import threading
import time
from tkinter import *
# 定义服务器信息
HOST = ''
PORT = 8888
ADDRESS = (HOST, PORT)
BUFFER = 1024
ck = None # 用于储存客户端的信息
win = tkinter.Tk() # 创建主窗口
win.title('模拟服务器')
win.geometry("400x500")
win.resizable(width=False, height=False) # 宽不可变, 高可变,默认为True
users = {} # 用户字典,也可以连接数据库
def run(ck, ca):
user_name = ck.recv(1024) # 接受客户端发送的信息以1k作为单位这里接受到的信息为byte类型
users[user_name.decode("utf-8")] = ck # 解码并储存用户的信息
print_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n'+""\
+ user_name.decode("utf-8") + ".....已成功连接\n" # 在连接显示框中显示是否连接成功
text1.insert(tkinter.INSERT, print_str)
for i in users:
for j in users:
users[i].send(j.encode("utf-8"))
while True:
r_data = ck.recv(BUFFER) # 接受客户端发送的信息以1k作为单位这里接受到的信息为byte类型
data_str = r_data.decode("utf-8")
infolist = data_str.split(":") # 分割字符串从而得到谁发送的用户名和客户端所发送的信息
text2.insert(tkinter.INSERT, time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) +
'\n' + user_name.decode("utf-8")+':' + infolist[1])
users[infolist[0]].send((time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n' + user_name.decode("utf-8")
+ ":" + infolist[1]).encode("utf-8"))
# 要发送信息的客户端向目标客户端发送信息
def start():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDRESS)
server.listen(128) # 设置监听,和设置连接的最大的数量
print_str = "服务器启动成功\n" # ,是否连接成功
text1.insert(tkinter.INSERT, print_str) # 显示在信息窗口中
while True: # 这里用死循环是因为模拟的服务器要一直运行
ck, ca = server.accept() # 接受所连接的客户端的信息
# 其中ca是ip和端口号组成的元组,ck有关客户端的信息
t = threading.Thread(target=run, args=(ck, ca)) # 每连接一个客户端就开启一个线程
# 其中Thread函数中的传入函数的参数也是以元组的形式
t.start() # 开启线程
def start_sever():
root.deiconify()
win.withdraw()
s = threading.Thread(target=start) # 启用一个线程开启服务器
s.start() # 启线程
def _clear():
entryIp.delete(0, END)
# 界面展示
labelIp = tkinter.Label(win, text='IP:', font=('楷体', 18))
labelIp.place(x=100, y=100)
eip = tkinter.Variable()
entryIp = tkinter.Entry(win, textvariable=eip, font=('楷体', 16), width=17)
entryIp.place(x=150, y=106)
labelPort = tkinter.Label(win, text='PORT:', font=('楷体', 18))
labelPort.place(x=75, y=150)
e_port = tkinter.Variable()
entryPort = tkinter.Entry(win, textvariable=e_port, font=('楷体', 16), width=17)
entryPort.place(x=150, y=155)
button1 = tkinter.Button(win, text="启动", command=start_sever, font=('楷体', 14))
button1 .place(x=250, y=200)
button2 = tkinter.Button(win, text="清除", command=_clear, font=('楷体', 14))
button2.place(x=180, y=200)
che = tkinter.Checkbutton(win, width=100, height=20, text='22222')
che.place(x=180, y=300)
# 创建子窗口
root = tkinter.Tk()
root.withdraw()
root.title('服务端信息展示')
root.geometry("400x500")
root.resizable(width=False, height=False) # 宽不可变, 高可变,默认为True
show_online = tkinter.Label(root, text='在线好友', font=('楷体', 16))
show_online.place(x=150, y=10)
text1 = tkinter.Text(root, height=10, width=40, font=('楷体', 15))
text1.place(x=0, y=35)
show_mess = tkinter.Label(root, text='信息记录', font=('楷体', 16))
show_mess.place(x=150, y=240)
text2 = tkinter.Text(root, height=10, width=40, font=('楷体', 15))
text2.place(x=0, y=270)
root.mainloop()
win.mainloop()
运行截图:
点击启动按钮之后截图: