码迷,mamicode.com
首页 > Web开发 > 详细

基于Tcp协议的上传下载

时间:2018-08-15 22:46:06      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:imp   __file__   src   sage   utf-8   技术   基于   encode   code   

目录格式:

  技术分享图片

构建此目录就可随意使用! 

 

client端

import socket
import sys
import os
import json
import struct

sk = socket.socket()
adress = (‘192.168.12.11‘, 29231)


def upload():
    message = {‘filename‘: None, ‘filesize‘: None}
    sk.connect(adress)
    temp = ‘上传‘
    sk.send(temp.encode(‘utf-8‘))
    meg = input(‘输入文件路径:‘)
    filename = os.path.basename(meg)
    filesize = os.path.getsize(meg)
    message[‘filename‘] = filename
    message[‘filesize‘] = filesize
    str_message = json.dumps(message)
    len_message = len(str_message)
    b_len_message = struct.pack(‘i‘, len_message)
    sk.send(b_len_message + str_message.encode(‘utf-8‘))
    with open(meg, ‘rb‘) as f:
        while filesize:
            connent = f.read()
            sk.send(connent)
            filesize -= len(connent)


def download():
    path = os.path.dirname(os.path.abspath(__file__))
    sk.connect(adress)
    temp = ‘下载‘
    sk.send(temp.encode(‘utf-8‘))
    num = sk.recv(1024).decode(‘utf-8‘)
    print(‘可下载文件:%s‘ % (num,))
    name = input(‘输入下载文件名称:‘)
    sk.send(name.encode(‘utf-8‘))
    path = ‘/‘.join([path, ‘本地文件‘, name])
    b_len_message = sk.recv(4)
    len_message = struct.unpack(‘i‘, b_len_message)[0]
    res_s = sk.recv(len_message).decode(‘utf-8‘)
    str_message = json.loads(res_s)
    filesize = str_message[‘filesize‘]
    with open(path, ‘ab‘) as f:
        while filesize:
            connent = sk.recv(1024)
            f.write(connent)
            f.flush()
            filesize -= len(connent)


def main():
    dic = [
        (‘上传‘, ‘upload‘),
        (‘下载‘, ‘download‘)
    ]
    for i in enumerate(dic, 1):
        print(i[0], i[1][0])
    num = int(input(‘请输入选项:‘))
    getattr(sys.modules[__name__], dic[num - 1][1])()
    sk.close()


if __name__ == ‘__main__‘:
    main()

 

 server端

import socket
import struct
import os
import json

path = os.path.abspath(__file__)
path = os.path.dirname(path)
sk = socket.socket()
adress = (‘0.0.0.0‘, 29231)
sk.bind(adress)
sk.listen(4)
conn, addr = sk.accept()


def upload(path):
    b_len_message = conn.recv(4)
    len_message = struct.unpack(‘i‘, b_len_message)[0]
    res_s = conn.recv(len_message).decode(‘utf-8‘)
    str_message = json.loads(res_s)
    filename = ‘1‘ + str_message[‘filename‘]
    filesize = str_message[‘filesize‘]
    path = ‘/‘.join([path, ‘客户端上传的文件‘, filename])
    with open(path, ‘ab‘) as f:
        while filesize:
            connent = conn.recv(1024)
            f.write(connent)
            f.flush()
            filesize -= len(connent)


def download(path):
    message = {‘filename‘: None, ‘filesize‘: None}
    res = os.listdir(‘/‘.join([path, ‘客户端上传的文件‘]))
    sum = ‘‘
    for i in res:
        sum = sum + ‘ ‘ + i + ‘    ‘
    conn.send(sum.encode(‘utf-8‘))
    filename = conn.recv(1024).decode(‘utf-8‘)
    path = ‘/‘.join([path, ‘客户端上传的文件‘, filename])
    filesize = os.path.getsize(path)
    message[‘filename‘] = path
    message[‘filesize‘] = filesize
    str_message = json.dumps(message)
    len_message = len(str_message)
    b_len_message = struct.pack(‘i‘, len_message)
    conn.send(b_len_message + str_message.encode(‘utf-8‘))

    with open(path, ‘rb‘) as f:
        while filesize:
            connent = f.read()
            conn.send(connent)
            filesize -= len(connent)


def main():
    res_s = conn.recv(1024).decode(‘utf-8‘)
    if res_s == ‘上传‘:
        upload(path)
        sk.close()
    if res_s == ‘下载‘:
        download(path)
        sk.close()


if __name__ == ‘__main__‘:
    while 1:
        main()

 

技术分享图片

 

 

技术分享图片

 

 

技术分享图片

 

基于Tcp协议的上传下载

标签:imp   __file__   src   sage   utf-8   技术   基于   encode   code   

原文地址:https://www.cnblogs.com/xiao-xue-di/p/9484096.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!