Python简易聊天室
本文最后更新于 141 天前,其中的信息可能已经有所发展或是发生改变。

这是第二个

一、一对一版

1).服务端

我又倦了,直接贴代码

import socket
import threading
s = socket.socket()
s.bind(("", 2333))
s.listen(5)
nick = input("输入昵称:")
print("Start Server...")
sock, addr = s.accept()
print(addr, " Connected")
print("输入q以退出")
other = sock.recv(1024).decode()
def reciver():
    while 1:
        data = sock.recv(1024)
        print(other, " : ", data.decode())
def sender():
    while 1:
        sock.send(nick.encode())
        massage = input()
        if massage == 'q':
            break
        print(nick + " : " + massage)
        sock.send(massage.encode())
t1 = threading.Thread(target=reciver, name='recive')
t2 = threading.Thread(target=sender, name='send')
t1.start()
t2.start()
t2.join()
sock.close()
print("断开连接")

2).客户端

import socket
import threading
s = socket.socket()
nick = input("输入昵称:")
ip = input("输入服务器ip:")
port = 2333
s.connect((ip, port))
print("已连接服务器\n输入q以退出")
s.send(nick.encode())
def sender():
    while 1:
        massage = input()
        if massage == 'q':
            break
        print(nick + " : " + massage)
        s.send(massage.encode())
def reciver():
    while 1:
        other = s.recv(1024).decode()
        massage = s.recv(1024).decode()
        print(other, " : ", massage)
t1 = threading.Thread(target=reciver, name='recive')
t2 = threading.Thread(target=sender, name='send')
t1.start()
t2.start()
t2.join()
print("断开连接")
s.close()

二、多人聊天室

1).服务端

import socket
import threading
user={}
address={}
def send(text,addr):
    for i in user:
        try:
            if addr != '' and i == address[addr]:
                pass
            else:
                s.sendto(text.encode(),user[i])
        except:
            logout(i)
def preload():
    while 1:
        preloading=s.recvfrom(1024)
        mess=preloading[0].decode()
        addr=preloading[1]
        if addr in address:
            try:
                if mess == 'q':
                    logout(address[addr])
                else:
                    send(address[addr]+ ':' + mess,addr)
            except:
                logout(address[addr])
        else:
            login(mess,addr)
            print(addr," Connected\n当前在线人数:",len(user))
def login(nick,addr):
    if nick in user:
        s.sendto("系统消息:用户名存在\n请重新输入:".encode(),addr)
    else:
        user[nick]=addr
        address[addr]=nick
        send("系统消息:欢迎"+nick+"进入直播间",'')
def logout(nick):
    send("系统消息:"+nick+"褪出了直播间",'')
    s.sendto("sys_exit".encode(),user[nick])
    print(user[nick],"Logout\n当前在线人数:",len(user)-1)
    del address[user[nick]]
    del user[nick]
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(('',2333))
print("Start Server...")
thread=threading.Thread(target=preload)
thread.setDaemon(True)
thread.start()
thread.join()

2).客户端

import socket
import threading
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
nick = input("输入昵称:")
ip = input("输入服务器ip:")
port = 2333
s.connect((ip,port))
print("连接服务器...\n如需退出,按q(请务必不要强制退出)")
s.send(nick.encode())
def sender():
    try:
        while 1:
            massage = input()
            s.sendto(massage.encode(),(ip,port))
            if massage == 'q':
                exit()
    except:exit()
def reciver():
    try:
        while 1:
            massage=s.recvfrom(1024)[0].decode()
            if massage == 'sys_exit':
                break
            print(massage)
    except:print("IF YOU MEET SOME TROUBLE,Please Contact Developer\nBye~")
t1 = threading.Thread(target=reciver, name='recive')
t2 = threading.Thread(target=sender, name='send')
t1.start()
t2.start()
t2.join()

…不想写了…明天再说,我去康康有什么新优惠

暂无评论

发送评论 编辑评论


				
上一篇
下一篇