代码拉取完成,页面将自动刷新
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File : TCP.py
@Contact : admin@lightr.cn
@License : (C)Copyright 2017-2021
@Modify Time @Author @Version @Desciption
------------ ------- -------- -----------
2021/10/31 21:03 @乘风小栈 1.0 None
"""
from concurrent.futures import ThreadPoolExecutor
from socket import *
import tkinter as tk
class Chat:
def __init__(self):
self.num = 0
self.flag = 0 # 0: 客户端 1:服务端
self.Thread = ThreadPoolExecutor(3)
self.server_socket = None
self.server_address = None
self.client_socket = None
self.client_address = None
self.uiOutput = None
self.uiInput = None
self.uiSend = None
self.uiPort = None
self.uiIp = None
self.uiLocal = None
self.uiConnect = None
self.Name = "匿名"
def init(self):
ip = self.uiIp.get('0.0', 'end').replace("\n", "")
port = self.uiPort.get('0.0', 'end').replace("\n", "")
if port != "" and ip == "":
print("走服务端")
self.AsService("", int(port))
elif port != "" and ip != "":
print("走客户端")
self.AsClient(ip, int(port))
else:
print("请输入IP 或 Port")
def AsService(self, ip, port):
print("=======服务端=========")
self.flag = 1
self.Name = "服务端"
try:
self.server_socket = socket(AF_INET, SOCK_STREAM)
self.server_socket.bind(("", port))
self.server_socket.listen(5)
print("===进入等待==")
self.client_socket, self.client_address = self.server_socket.accept()
print("==客户端连接成功==")
self.uiConnect.configure(text="连接成功!")
self.Thread.submit(self.ServiceReceives)
except BaseException as f:
print(f)
self.server_socket.close()
def ServiceReceives(self):
print("===进入服务端接收状态====")
try:
while True:
data = self.client_socket.recv(2048)
txt = data.decode('utf-8')
print(txt)
self.uiOutput.insert(tk.INSERT, f"{txt}\n", "tag_2")
except BaseException as f:
print(f)
self.client_socket.close()
def AsClient(self, ip, port):
print("=======客户端=========")
self.flag = 0
self.Name = "客户端"
try:
self.client_socket = socket(AF_INET, SOCK_STREAM)
self.client_socket.connect((ip, port))
except BaseException as f:
print(f)
self.client_socket.close()
print("==连接成功==")
self.uiConnect.configure(text="连接成功!")
self.Thread.submit(self.ClientReceives)
def ClientReceives(self):
print("===进入客户端接收状态====")
try:
while True:
data, ADDR = self.client_socket.recvfrom(2048)
txt = data.decode('utf-8')
print(txt)
self.uiOutput.insert(tk.INSERT, f"{txt}\n")
# 操作客户端得到的数据
except BaseException as f:
print(f)
self.client_socket.close()
def Send(self, txt):
data = f"{self.Name}:{txt}".encode("utf-8")
self.client_socket.send(data)
self.num += 1
def SendTxt(self):
txt = self.uiInput.get('0.0', 'end').replace("\n", "")
self.Send(txt)
self.uiOutput.insert(tk.INSERT, f"{self.Name}:{txt}\n")
self.uiInput.delete(0.0, tk.END)
# 显示本机IP
def showLocalIp(self):
hostname = gethostname()
# 获取本机IP
ip = gethostbyname(hostname)
self.uiLocal.configure(text=ip)
# 创建UI执行的一些东西
def creatUIFun(self):
self.showLocalIp()
# ..可以自定义
def creatUI(self):
root = tk.Tk()
root.title("TCP聊天软件")
f1 = tk.Frame(width=300, height=300)
f2 = tk.Frame(width=200, height=200)
f3 = tk.Frame(width=100, height=200)
f4 = tk.Frame(f3, width=100, height=200)
f5 = tk.Frame(f3, width=100, height=200)
# 聊天框
self.uiOutput = tk.Text(f1, width=100, height=30, font=18)
self.uiOutput.pack()
# 发送按钮和输入框
self.uiInput = tk.Text(f2, width=70, height=10, font="4")
self.uiInput.pack()
self.uiSend = tk.Button(f3, text="发送", width=30, height=2, font="4", command=self.SendTxt)
self.uiSend.pack()
# IP输入框
tk.Label(f4, text="IP地址:", width=10, height=2).pack(side=tk.LEFT)
self.uiPort = tk.Text(f4, width=4, height=2, font=2)
self.uiPort.pack(side=tk.RIGHT)
self.uiIp = tk.Text(f4, width=15, height=2, font=2)
self.uiIp.pack(side=tk.RIGHT)
# 本机IP提示框
tk.Label(f5, text="本机IP:", width=10, height=2).pack(side=tk.LEFT)
self.uiLocal = tk.Label(f5, text="192.168.1.1", width=20, height=2)
self.uiLocal.pack(side=tk.LEFT)
self.uiConnect = tk.Button(f3, text="连接", width=30, height=2, font=4,
command=lambda: self.Thread.submit(self.init))
self.uiConnect.pack()
# 小窗口打包
f1.pack()
f2.pack(side=tk.LEFT)
f3.pack(side=tk.RIGHT)
f4.pack()
f5.pack()
self.creatUIFun()
root.mainloop()
t = Chat()
t.creatUI()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。