• python socket 登录文件下载 合并


    client 

    import os
    import sys
    import json
    import struct
    import socket
    def download(sk): # 下载

    opt_dic = {'operate':'download'}
    my_send(sk,opt_dic)
    msg = my_recv(sk)
    with open(msg['filename'], 'wb') as f:
    while msg['filesize'] > 0:
    content = sk.recv(1024)
    msg['filesize'] -= len(content)
    f.write(content)

    def login(sk):
    while True:
    usr = input('用户名:').strip()
    pwd = input('密码:').strip()
    dic = {'username':usr,'password':pwd}
    my_send(sk,dic)
    ret = my_recv(sk)
    if ret['operate'] == 'login' and ret['result']:
    print('登陆成功')
    break
    else:
    print('登陆失败')

    def my_send(sk,dic): #发送
    str_dic = json.dumps(dic)
    b_dic = str_dic.encode('utf-8')
    mlen = struct.pack('i',len(b_dic))
    sk.send(mlen)# 4个字节 表示字典转成字节之后的长度
    sk.send(b_dic)# 具体的字典数据

    def my_recv(sk):#接收
    msg_len = sk.recv(4)
    dic_len = struct.unpack('i',msg_len)[0]
    msg = sk.recv(dic_len).decode('utf-8')
    msg = json.loads(msg)
    return msg

    sk = socket.socket()
    sk.connect(('127.0.0.1',9000))
    #登陆
    login(sk)
    #下载
    opt_lst = ['upload','download']
    for index,opt in enumerate(opt_lst,1):#遍历opt_lst,序号从1开始
    print(index,opt)
    num = int(input('请选择你要操作的序号:'))
    getattr(sys.modules[__name__],opt_lst[num-1])(sk)
    sk.close()

    server

    
    
    import os
    import sys
    import json
    import struct
    import socket
    import hashlib

    def login(conn):
    flag = True
    while True:
    #denglu
    msg = my_recv(conn)
    with open('userinfo') as f:
    for line in f :
    name,pwd = line .strip().split('|')
    if name == msg['username'] and pwd == get_md5(name,msg['password']):
    res,flag = True,False
    break
    else:
    res = False
    dic = {'operate':'login','result':res}
    my_send(conn,dic)
    def my_send(conn,dic):
    str_dic = json.dumps(dic)#字典形式的数据转化为字符串
    b_dic = str_dic.encode('utf-8')
    mlen = struct.pack('i',len(b_dic))
    conn.send(mlen) # 4个字节 表示字典转成字节之后的长度
    conn.send(b_dic) # 具体的字典数据

    def download():
    abs_path = r'D:学生宿舍管理系统练习day321.作业讲解.mp4'
    filename = os.path.basename(abs_path)#返回path最后的文件名
    filesize = os.path.getsize(abs_path)#获得文件的大小(字节)
    dic ={'filename':filename,'filesize':filesize}
    my_send(conn,dic)
    with open(abs_path,mode='rb') as f:
    while filesize > 0:
    con= f.read(1024)
    filesize -= len(con)
    conn.send(con)
    def my_recv(conn):
    msg_len = conn.recv(4)
    dic_len = struct.unpack('i',msg_len)[0]#解包 转换成python 数据类型 i 为int类型
    msg =conn.recv(dic_len).decode('utf-8')
    msg = json.loads(msg)
    return msg

    def get_md5(username,password):
    md5 = hashlib.md5(username.encode('utf-8'))
    md5.update(password.encode('utf-8'))
    return md5.hexdigest()
    #接收
    sk =socket.socket()
    sk.bind(('127.0.0.1',9000))
    sk.listen()
    conn,_ = sk.accept()
    login(conn)
    # 接收消息,根据用户的选择进行上传/下载操作
    opt_dic = my_recv(conn)
    if hasattr(sys.modules[__name__],opt_dic['operate']):#sys.modules[__name__]找到当前文件下对象
    getattr(sys.modules[__name__],opt_dic['operate'])()
    conn.close()
    sk.close()


  • 相关阅读:
    css中的属性
    css初识和css选择器
    前端html的简单认识
    数据库进阶了解
    数据库索引
    pymysql模块
    数据库的多表查询
    数据库中的行操作
    数据库和表操作以及完整性约束
    数据库概述
  • 原文地址:https://www.cnblogs.com/shaohuagu/p/12266923.html
Copyright © 2020-2023  润新知