码迷,mamicode.com
首页 > 编程语言 > 详细

python——基础篇-简易文件上传

时间:2019-09-09 22:23:46      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:input   end   pen   info   filename   serve   成功   NPU   server   

post_server

import socket
import os
sk=socket.socket()
address=(127.0.0.1,8000)
sk.bind(address)
sk.listen(3)
print(waiting...)
BASE_DIR=os.path.dirname(os.path.abspath(__file__))

while 1:
    conn,addr=sk.accept()
    while 1:
        data=conn.recv(1024)
        cmd,filename,filesize=str(data,utf-8).split(|)
        path=os.path.join(BASE_DIR,post,filename)
        filesize=int(filesize)

        f=open(path,ab)
        has_receive=0
        while has_receive!=filesize:
            data=conn.recv(1024)
            f.write(data)
            has_receive+=len(data)
        f.close()

 

post_client:

import socket
import os
sk=socket.socket()
address=(127.0.0.1,8000)

sk.connect(address)

BASE_DIR=os.path.dirname(os.path.abspath(__file__))


while True:
    inp=input(>>>).strip()

    cmd,path=inp.split(|)

    path = os.path.join(BASE_DIR,path)

    filename = os.path.basename(path)

    file_size = os.stat(path).st_size

    file_info = post|%s|%s %(filename,file_size)

    sk.sendall(bytes(file_info, utf-8))

    f=open(path, rb)
    data = f.read(1024)
    has_sent = 0
    while has_sent!=file_size:
        data = f.read(1024)
        sk.sendall(data)
        has_sent += len(data)


f.close()
print(上传成功)

 

python——基础篇-简易文件上传

标签:input   end   pen   info   filename   serve   成功   NPU   server   

原文地址:https://www.cnblogs.com/zzzi/p/11494533.html

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