Python3⽹络通信⽹络聊天室⽂件传输Python3 ⽹络通信 ⽹络聊天室 ⽂件传输
功能描述
该项⽬将实现⼀个⽂字和⽂件传输的客户端和服务器程序通信应⽤程序。它将传输和接收视频⽂件。
⽂本消息必须通过TCP与服务器通信,⽽客户端⾃⼰⽤UDP传输视频⽂件。
程序将⽀持⼀下功能:
⽤户⾝份验证
向服务器发布消息
编辑或删除消息、读取消息
从服务器发送消息
读取活动⽤户的信息
上传视频⽂件从⼀个⽤户到另⼀个⽤户
简要流程
该项⽬将实现各种应⽤程序协议来实现以上功能。
服务器将监听指定为命令⾏参数的端⼝,并等待客户端监听连接。
客户端程序将发起⼀个与服务器的TCP连接。
在连接建⽴后,⽤户将启动⾝份验证过程。客户端将与⽤户交互通过命令⾏界⾯。
⾝份验证成功后,⽤户可以执⾏⼀系列命令
最终登出
客户端和服务器都必须打印有意义的提⽰消息
简要设计
Server: 使⽤⼀个类来保存每个客户端信息,包括⽤户名和IP地址的映射,并多线程开始接收每个客户端的处理程序。
Client: 三个线程:主线程为获取⽤户输⼊和发送到服务器,和⼀个线程为接收服务器响应,⼀个线程UDP传输⽂件功能协议
Login: flag("Login") + username + password
MSG: flag("MSG") + message
EDT: flag("EDT") + msg_index + msg_time + new_msg
DLT: flag("DLT") + msg_index + msg_time
RDM: flag("RDM") + msg_time
ATU: flag("ATU")
OUT: flag("OUT")
UPD: flag("UDP") + username + filename
Usage
Server:python server.py 端⼝号最⼤密码尝试次数
image.png
image.png 代码
client
import socket
import threading
import time
import sys
import os
UDP_port = 6666
LoginStatus = False
TCP_running = True
clientSocket = socket.socket()
login_Event = threading.Event()
response_Event = threading.Event()
user_list = []
USERNAME = ""
# get IP address and port after ATU command
def get_address_port_by_username(name):
if len(user_list) == 0:
print("[error] need run ATU command first")
else:
for i in user_list:
user_info = i.split(", ")
username = user_info[0]
address = user_info[1]
port = user_info[2]
if username == name:
return address, port
return None
# LOGIN
def log_in(clientSocket,udp_port):
global USERNAME
print("Username: ", end="")
name = input('')
print("Password: ", end="")
pwd = input('')
FLAG = "LOGIN"
USERNAME = name
PWD = pwd
login_msg = FLAG + "%%" + name + "%%" + PWD + "%%" + str(udp_port)
clientSocket.send(de('utf-8'))
def reponse_to_client(recv_msg):
global TCP_running
global LoginStatus
global login_Event
send_msg = ""
recv_msg_list = []
recv_msg_list = recv_msg.split("%%")
res = True
if len(recv_msg_list) != 0:
if len(recv_msg_list) != 0:
flag = recv_msg_list[0]
if (flag == "LOGIN"):
TYPE = recv_msg_list[1]
if(TYPE == "0"):
LoginStatus = True
else:
LoginStatus = False
# inform the main loop the result from server
login_Event.set()
print(recv_msg_list[2])
elif (flag == "MSG"):
print(recv_msg_list[1])
elif flag == "EDT":
TYPE = recv_msg_list[1]
if (TYPE == "0"):
edit_res = True
else:
edit_res = False
print(recv_msg_list[2])
elif (flag == "RDM"):
txt = recv_msg_list[1:]
for i in txt:
print(i)
pass
elif (flag == "DLT"):
TYPE = recv_msg_list[1]
if (TYPE == "0"):
del_res = True
else:
del_res = False
print(recv_msg_list[2])
pass
elif (flag == "ATU"):
NUM = recv_msg_list[1]
if NUM == "0":
print(recv_msg_list[2])
else:
txt = recv_msg_list[2:]
for i in txt:
print(i)
user_list.append(i)
elif (flag == "OUT"):
TCP_running = False
print(recv_msg_list[1])
def read_server(s):
global response_Event
global TCP_running
while TCP_running:
# the thread always receive from server
content = s.recv(2048).decode('utf-8')
reponse_to_client(content)
# inform the mian loop, resume it
response_Event.set()
# s.close()
os._exit(1)
def execute_command(clientSocket):
response_Event.clear()
while TCP_running:
print("Enter one of the following commands (MSG, DLT, EDT, RDM, ATU, OUT, UPD): ", end="")        msg = input('')
if not msg:
continue
command = msg.split(" ")[0]
if command == "MSG":
if len(msg.split(" ")) < 2:
print("need arguments")
continue
txt = msg.split(" ")[1:]
msg = command + "%%" + ' '.join(txt)
elif command == "EDT":
if len(msg.split(" ")) < 2:
print("[error] need arguments")
continue
args = msg.split(" ")[1:]
index = args[0][1:]
dt = ' '.join(args[1:5])
txt = ' '.join(args[5:])
msg = command + "%%" + index + "%%" + dt + "%%" + txt
elif command == "DLT":
if len(msg.split(" ")) < 2:
print("[error] need arguments")
continue
args = msg.split(" ")[1:]
index = args[0][1:]
dt = ' '.join(args[1:])
msg = command + "%%" + index + "%%" + dt
pass
elif command == "RDM":
if len(msg.split(" ")) < 2:socket编程聊天室基本流程
print("[error] need arguments")
continue
dt = ' '.join(msg.split(" ")[1:])
msg = command + "%%" + dt
elif command == "ATU":
if len(msg.split(" ")) < 2:
pass
else:
print("[error] too many arguments")
continue
pass
elif command == "OUT":
if len(msg.split(" ")) < 2:
pass
else:
print("[error] too many arguments")
continue
pass
elif command == "UPD":
if len(msg.split(" ")) < 3:
print("[error] need more arguments")
continue
else:
UDP_send(msg.split(" ")[1], msg.split(" ")[2])
continue
else:
print("[error] unknown command")
continue
clientSocket.de('utf-8'))
# block, waiting for server response
time.sleep(0.3)
response_Event.wait(30)
response_Event.clear()
# mian func
def tcp_start(server_IP, server_port, udp_port):
global clientSocket
clientSocket = socket.socket()
TCP_t = threading.Thread(target=read_server, args=(clientSocket,)).start()    # login
while LoginStatus == False:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。