标签:eve rom 字节 bin add and lib 路径 str
# 1 .黏包现象 发送的两个数据黏在一起 #2 .成因 #发送端粘:和包机制 #接受端粘 :接受不及时 #数据与数据是无边界的流逝传输 # 3解决黏包 #自定义协议 #struct模块 #把任意长度的数据变成固定的4个字节 #低级 #先发送数据长度 #在发送数据 #高级 #先把所有相发送的数据信息放在字典 #发送字典的长度 #发送字典 #发送设计的数据 #作业 #1 默写 黏包协议 #2 上传大文件 视频 文件 图片 # 4个g #3 和你同桌 调通从你的计算机上传一个视频到你同桌电脑 #4 进阶 : 带上登录 先登录 在上传
登录:
import sys import json import socket import hashlib def get_md5(username,password): md5 = hashlib.md5(username.encode(‘utf-8‘)) md5.update(password.encode(‘utf-8‘)) return md5.hexdigest() def login(dic_msg): print(dic_msg[‘user‘], dic_msg[‘pwd‘]) with open(‘userinfo‘, encoding=‘utf-8‘) as f: for line in f: user, pwd = line.strip().split(‘|‘) print(pwd,get_md5(dic_msg[‘user‘], dic_msg[‘pwd‘])) if user == dic_msg[‘user‘] and pwd == get_md5(dic_msg[‘user‘], dic_msg[‘pwd‘]): return {‘opt‘:‘login‘,‘result‘:True} else:return {‘opt‘:‘login‘,‘result‘:False} sk= socket.socket() sk.bind((‘127.0.0.1‘,9001)) sk.listen() while True: conn,addr = sk.accept() msg = conn.recv(1024).decode(‘utf-8‘) dic_msg = json.loads(msg) if hasattr(sys.modules[__name__],dic_msg[‘operate‘]): ret = getattr(sys.modules[__name__],dic_msg[‘operate‘])(dic_msg) content = json.dumps(ret).encode(‘utf-8‘) conn.send(content) conn.close() sk.close()
import json import socket import hashlib def get_md5(username,password): md5 = hashlib.md5(username[::2].encode(‘utf-8‘)) md5.update(password.encode(‘utf-8‘)) return md5.hexdigest() username = input(‘用户名 :‘) password = input(‘密 码 :‘) sk = socket.socket() sk.connect((‘127.0.0.1‘,9001)) msg = {‘operate‘:‘login‘, ‘user‘:username, ‘pwd‘:get_md5(username,password)} str_msg = json.dumps(msg) sk.send(str_msg.encode(‘utf-8‘)) content = sk.recv(1024) str_content = content.decode(‘utf-8‘) dic_content = json.loads(str_content) if dic_content[‘result‘]: print(‘登录成功‘) else: print(‘登录失败‘) sk.close()
usrinfo:
alex|ee838c58e5bb3c9e687065edd0ec454f
一对多聊天:
import json import socket sk = socket.socket(type=socket.SOCK_DGRAM) sk.bind((‘127.0.0.1‘,9001)) user_info = { 1234:(‘alex‘,‘\033[1;32m‘), 5678:(‘宝元‘,‘\033[1;31m‘), } while True: msg,addr = sk.recvfrom(1024) str_msg = msg.decode(‘utf-8‘) dic_msg = json.loads(str_msg) code = dic_msg[‘code‘] content = dic_msg[‘msg‘] print(‘%s%s : %s\033[0m‘%(user_info[code][1],user_info[code][0],content)) ret = input(‘>>>‘).encode(‘utf-8‘) sk.sendto(ret,addr) sk.close()
import json import socket sk = socket.socket(type=socket.SOCK_DGRAM) addr = (‘127.0.0.1‘,9001) code = 5678 while True: msg = input(‘>>>‘) if msg.upper() == ‘Q‘: break send_msg = {‘code‘:code,‘msg‘:msg} str_msg = json.dumps(send_msg) sk.sendto(str_msg.encode(‘utf-8‘),addr) ret = sk.recv(1024).decode(‘utf-8‘) if ret.upper() == ‘Q‘: break print(ret) sk.close()
import json import socket sk = socket.socket(type=socket.SOCK_DGRAM) addr = (‘127.0.0.1‘,9001) code = 1234 while True: msg = input(‘>>>‘) if msg.upper() == ‘Q‘:break send_msg = {‘code‘:code,‘msg‘:msg} str_msg = json.dumps(send_msg) sk.sendto(str_msg.encode(‘utf-8‘),addr) ret = sk.recv(1024).decode(‘utf-8‘) if ret.upper() == ‘Q‘:break print(ret) sk.close()
黏包server
import struct import socket sk = socket.socket() sk.bind((‘127.0.0.1‘,9001)) sk.listen() conn,addr = sk.accept() num = conn.recv(4) num = struct.unpack(‘i‘,num)[0] file_name = conn.recv(num).decode(‘utf-8‘) filesize = conn.recv(4) filesize = struct.unpack(‘i‘,filesize)[0] with open(file_name,‘wb‘) as f: content = conn.recv(filesize) f.write(content) conn.close() sk.close()
import os import struct import socket sk = socket.socket() sk.connect((‘127.0.0.1‘,9001)) filepath = input(‘请输入文件路径 :‘) filename = os.path.basename(filepath).encode(‘utf-8‘) name_len = struct.pack(‘i‘,len(filename)) sk.send(name_len) sk.send(filename) filesize = os.path.getsize(filepath) file_len = struct.pack(‘i‘,filesize) sk.send(file_len) with open(filepath,‘rb‘) as f: content = f.read() sk.send(content) sk.close() # 先发送字典的长度 # 再发字典 {‘filename‘:xxxx,‘filesize‘:xxxxx} # 再发文件内容
标签:eve rom 字节 bin add and lib 路径 str
原文地址:https://www.cnblogs.com/Doner/p/10682760.html