📅  最后修改于: 2023-12-03 15:19:54.915000             🧑  作者: Mango
samspendchat
是一款现代化的、基于 Python 的即时通信软件。它使用了 Socket 编程,可实现多人聊天,支持文字和图片消息发送。本文将为您介绍该软件的主要特点、使用方法以及代码示例,并提供必要的代码片段以便集成到您的应用程序中。
使用 samspendchat
可以分为两个部分:服务器端和客户端。
sampsendchat
的源代码python server.py
python client.py [server_ip] [port]
8088
。from server import Server
if __name__ == '__main__':
server = Server()
server.start()
from client import Client
if __name__ == '__main__':
client = Client('localhost', 8088)
client.run()
以下代码片段可供您参考,以便进行集成到自己的应用程序中。务必按照 markdown 标明。
from threading import Thread
import socket
class Server:
def __init__(self, host="localhost", port=8088):
self.clients = {}
self.addresses = {}
self.host = host
self.port = port
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.bind((self.host, self.port))
self.server.listen(5)
print("Waiting for connection...")
def handle_client(self, conn, addr):
while True:
try:
message = conn.recv(1024)
if message != bytes("{quit}", "utf8"):
self.broadcast(message, conn)
else:
self.remove(conn, addr)
conn.close()
break
except ConnectionResetError:
self.remove(conn, addr)
break
def broadcast(self, message, conn):
for client_socket in self.clients:
if client_socket != conn:
client_socket.send(message)
def remove(self, conn, addr):
del self.clients[conn]
del self.addresses[addr]
self.broadcast(bytes("{} has left the chat.".format(addr[0]), "utf8"), None)
def start(self):
print("Server has started on port " + str(self.port) + " " + "ip " + str(self.host))
while True:
conn, addr = self.server.accept()
print("{} has connected.".format(addr[0]))
self.broadcast(bytes("{} has joined the chat!".format(addr[0]), "utf8"), None)
self.clients[conn] = addr
self.addresses[addr] = conn
Thread(target=self.handle_client, args=(conn, addr)).start()
from threading import Thread
import socket
from tkinter import *
class Client:
def __init__(self, host, port):
self.host = host
self.port = port
self.root = Tk()
self.root.geometry("500x500")
self.root.title("Chat Room")
messages_frame = Frame(self.root)
self.scrollbar = Scrollbar(messages_frame)
self.msg_list = Listbox(messages_frame, height=30, width=70, yscrollcommand=self.scrollbar.set)
self.scrollbar.pack(side=RIGHT, fill=Y)
self.msg_list.pack(side=LEFT, fill=BOTH)
self.msg_list.pack()
messages_frame.pack()
self.entry_field = Entry(self.root)
self.entry_field.pack()
self.entry_field.bind("<Return>", self.send)
self.send_button = Button(self.root, text="Send", command=self.send)
self.send_button.pack()
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.host, self.port))
def run(self):
Thread(target=self.receive).start()
mainloop()
def send(self, event=None):
message = self.entry_field.get()
self.entry_field.delete(0, END)
self.sock.send(bytes(message, "utf8"))
if message == "{quit}":
self.sock.close()
self.root.quit()
def receive(self):
while True:
try:
message = self.sock.recv(1024)
self.msg_list.insert(END, message.decode("utf8"))
except OSError:
break
samspendchat
是一个功能强大的现代化即时通信软件,支持多人聊天和不同类型的消息发送。上述文中介绍了该软件的主要特点、使用方法以及代码示例和代码片段。如果您想了解更多或有其他问题,请在 [GitHub][1] 上联系我们。谢谢!
[1]: https://github.com/hongqiang128/sampsendchat