• 文件的上传、下载结合用户认证并解决粘包问题


    # 1.文件的上传、下载
    # 2.结合用户认证
    # 要求:
    # 1.tcp协议粘包的问题,使用struct模块解决粘包问题
    # 2.传递的所有的信息都尽量是json格式
    # 3.再server端用上反射

    server端

    import os
    import json
    import time
    import socket


    def up():
    str_dic = conn.recv(1024).decode('utf-8') # 接收str字典
    if str_dic == 'Q':
    return
    dic = json.loads(str_dic) # 转换为字典
    print(dic)
    content = conn.recv(dic['filesize']) # 接收字典中的文件大小项
    filebase = os.path.join(r'D:homeworkday29file', dic['filename'])
    with open(filebase, mode='wb') as f: # 创建字典中文件名的项,模式为写字节
    f.write(content)


    def download():
    a = os.listdir(r'D:homeworkday29file') # 服务器上的文件的列表
    stra = json.dumps(a) # 字符串列表
    conn.send(stra.encode('utf-8')) # 发给client
    filename = conn.recv(1024).decode('utf-8') # 接收文件名
    file = os.path.join(r'D:homeworkday29file', filename) # 获取文件路径
    filesize = os.path.getsize(file)
    dic = {'filename': filename, 'filesize': filesize}
    strdic = json.dumps(dic)
    conn.send(strdic.encode('utf-8'))
    with open(file, mode='rb') as f: # 打开文件,模式为读取字节
    content = f.read()

    conn.send(content)
    print('下载完成!')


    def run(con):
    dic = {'1': up, '2': download}
    while 1:
    choice = con.recv(1024).decode('utf-8')
    print(choice)
    func = dic.get(choice)
    if choice.upper() == 'Q':
    break
    if not func:
    print('输入有误!')
    time.sleep(1)
    continue
    func()
    print('任务完成!')
    break


    sk = socket.socket()
    sk.bind(('127.0.0.1', 9000))
    sk.listen()
    while 1:
    conn, addr = sk.accept()
    run(conn)
    conn.close()


    sk.close()

    client端

    import os
    import json
    import socket


    def up():
    sk.send('1'.encode('utf-8'))
    file_path = input('>>>') # 输入文件路径
    if file_path.upper() == 'Q':
    sk.send(b'Q')
    return
    if os.path.isabs(file_path): # 判断文件路径是否为绝对路径
    filename = os.path.basename(file_path) # 返回文件名
    filesize = os.path.getsize(file_path) # 返回文件大小
    dic = {'filename': filename, 'filesize': filesize} # 存入字典
    str_dic = json.dumps(dic) # 转为str格式
    sk.send(str_dic.encode('utf-8'))
    with open(file_path, mode='rb') as f: # 打开文件,模式为读取字节
    content = f.read()
    sk.send(content)

    print('上传完成!')


    def download():
    sk.send(b'2')
    print('3333')
    msg = sk.recv(1024).decode('utf-8')
    article = json.loads(msg)
    for i in range(len(article)):
    print('序号:', i+1, '文件名:', article[i])
    user = int(input('请选择序号:'))
    sk.send(article[user-1].encode('utf-8'))
    strdic = sk.recv(1024).decode('utf-8')
    dic = json.loads(strdic)
    content = sk.recv(dic['filesize'])
    with open(dic['filename'], 'wb') as f:
    f.write(content)
    print('下载完成!')


    def run():
    dic = {'1': up, '2': download}
    while 1:
    user = input('1.上传文件 2.下载文件 请选择(Q/q退出):')
    if user.upper() == 'Q':
    break
    choice = dic.get(user)
    if not choice:
    print('输入有误!')
    continue
    dic[user]()
    print('任务完成程序结束!')
    return


    def regis():
    while 1:
    username = input('用户名:')
    pwd = input('密码:')
    if username == 'alex' and pwd == '123':
    print('登陆成功!')
    run()
    return
    else:
    print('账号或密码错误!')


    sk = socket.socket()
    sk.connect(('127.0.0.1', 9000))
    if __name__ == '__main__':
    regis()
    sk.close()
     
  • 相关阅读:
    HTML5标签
    CTF web之旅 45
    CTF web之旅44
    CTF web之旅 43
    CTF web之旅 42
    CTF web之旅41
    CTF web之旅40
    CTF web之旅 39
    CTF web之旅 38
    CTF web之旅 37
  • 原文地址:https://www.cnblogs.com/zjx1/p/10864530.html
Copyright © 2020-2023  润新知