zoukankan      html  css  js  c++  java
  • 文件上传下载代码

    #server端
    import socketserver
    import hashlib
    import struct
    import json
    import os
    import sys
    DownLoad_path = 'H:Python 学习文件大作业及复习上传'
    upload_path = 'H:Python 学习文件大作业及复习测试'
    file_list = ['测试-1.mp4','测试-2.mp4','测试-3.mp4']
    def my_send(conn,dic):
    jd = json.dumps(dic)
    res = struct.pack('i',len(jd))
    conn.send(res)
    conn.send(jd.encode('utf-8'))
    def my_recv(conn):
    res = conn.recv(4)
    res = struct.unpack('i',res)[0]
    dic = conn.recv(res).decode('utf-8')
    return json.loads(dic)
    def get_md5(user,pwd):
    m = hashlib.md5(user.encode('utf-8'))
    m.update(pwd.encode('utf-8'))
    return m.hexdigest()
    def login(conn):
    user_info = my_recv(conn)
    with open('userinfo') as f:
    for line in f:
    username,password = line.strip().split('|')
    print(username,password)
    if username == user_info['username'] and password == get_md5(user_info['username'],user_info['password']):
    result = True
    print(result)
    break
    else:
    result = False
    dic = {'operator': 'login', 'result': result}
    my_send(conn,dic)
    return result
    def download(conn):
    file_dic = my_recv(conn)
    path = os.path.join(DownLoad_path,file_dic['file_name'])
    print(path)
    with open(path,mode='wb') as f:
    while file_dic['file_size'] > 0:
    content = conn.recv(1024)
    f.write(content)
    file_dic['file_size'] -= len(content)
    print('文件上传完成')

    def upload(conn):
    my_send(conn,file_list)
    file_name = my_recv(conn)
    path = os.path.join(upload_path,file_name['file_name'])
    file_size = os.path.getsize(path)
    file_dic = {
    'file_name': file_name['file_name'],
    'file_size': file_size
    }
    my_send(conn, file_dic)
    with open(path, mode='rb') as f:
    while file_size > 0:
    content = f.read(1024)
    conn.send(content)
    file_size -= len(content)


    class Myserver(socketserver.BaseRequestHandler):
    def handle(self):
    conn = self.request
    while True:
    try:
    flag = login(conn)
    if flag:
    fun_dic = my_recv(conn)
    print(fun_dic)
    if fun_dic['function'] == 'download':
    getattr(sys.modules[__name__],fun_dic['function'])(conn)
    elif fun_dic['function'] == 'upload':
    getattr(sys.modules[__name__], fun_dic['function'])(conn)
    except OSError:
    break
    server = socketserver.ThreadingTCPServer(('127.0.0.1',9001),Myserver)
    server.serve_forever()



    #client端
    import socket
    import struct
    import json
    import os
    import sys
    upload_path = 'H:Python 学习文件大作业及复习下载'
    def my_send(sk,dic):
    jd = json.dumps(dic)
    res = struct.pack('i',len(jd))
    sk.send(res)
    sk.send(jd.encode('utf-8'))
    def my_recv(sk):
    res = sk.recv(4)
    res = struct.unpack('i',res)[0]
    dic = sk.recv(res).decode('utf-8')
    return json.loads(dic)
    def login():
    user = input('请输入你的用户名:').strip()
    pwd = input('请输入你的密码:').strip()
    user_info = {'username': user,'password' :pwd }
    my_send(sk,user_info)
    status_dic = my_recv(sk)
    if status_dic['result'] == True:
    print('登录成功')
    return True
    else:
    print('登录失败')
    return False
    def download(sk):
    path = input('请输入上传的文件地址:').strip()
    file_size = os.path.getsize(path)
    file_name = os.path.basename(path)
    file_dic = {
    'file_name' : file_name,
    'file_size' : file_size
    }
    my_send(sk,file_dic)
    with open(path,mode='rb') as f:
    while file_size > 0:
    content = f.read(1024)
    sk.send(content)
    file_size -= len(content)
    print('文件上传完成')
    def upload(sk):
    file_list = my_recv(sk)
    for index,file_name in enumerate(file_list,1):
    print(index,file_name)
    num = input('请选择要下载的内容:').strip()
    if num.isdecimal() and 0 < int(num) < 4:
    file_name = {'file_name' :file_list[int(num)-1]}
    my_send(sk,file_name)
    file_dic = my_recv(sk)
    path = os.path.join(upload_path, file_dic['file_name'])
    with open(path, mode='wb') as f:
    while file_dic['file_size'] > 0:
    content = sk.recv(1024)
    f.write(content)
    file_dic['file_size'] -= len(content)
    print('下载成功')

    sk = socket.socket()
    server_ip = ('127.0.0.1',9001)
    sk.connect(server_ip)
    oper_lst = ['download','upload']
    while True:
    ret = login()
    if ret:
    while True:
    print('请选择功能:')
    for index,opt in enumerate(oper_lst,1):
    print(index,opt)
    num = input('请输入你要选择的功能序号:').strip()
    fun_dic = {
    'function' : oper_lst[int(num)-1]
    }
    my_send(sk,fun_dic)
    if num.isdecimal() and 0 < int(num) < 3:
    print(oper_lst[int(num)-1])
    getattr(sys.modules[__name__],oper_lst[int(num)-1])(sk)




  • 相关阅读:
    fiddler 增加请求时间显示
    es 多字段分词查询优化
    【二分】【预处理】zoj4029 Now Loading!!!
    【数论】【扩展欧几里得】Codeforces Round #484 (Div. 2) E. Billiard
    【set】【multiset】Codeforces Round #484 (Div. 2) D. Shark
    【推导】Codeforces Round #484 (Div. 2) C. Cut 'em all!
    【递推】Codeforces Round #483 (Div. 2) [Thanks, Botan Investments and Victor Shaburov!] D. XOR-pyramid
    【数论】Codeforces Round #483 (Div. 2) [Thanks, Botan Investments and Victor Shaburov!] C. Finite or not?
    【Trie】【枚举约数】Codeforces Round #482 (Div. 2) D. Kuro and GCD and XOR and SUM
    【枚举】【贪心】Codeforces Round #482 (Div. 2) B. Treasure Hunt
  • 原文地址:https://www.cnblogs.com/iaoyuyuyuhuanghuang/p/14306507.html
Copyright © 2011-2022 走看看