socket版本檔案上傳

來源:互聯網
上載者:User

標籤:inpu   address   enc   bsp   操作   command   odi   send   put   

  upload_server.py

 1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import socket 4 import os 5 import sys 6  7  8  9 BASE_DIR = os.path.dirname(__file__)10 11 sk=socket.socket()12 sk.bind((‘127.0.0.1‘,8989))13 sk.listen(5)14 15 16 17 while True:18     try:19         conn,address = sk.accept()20         print(conn,address)21         command, filename, file_size = str(conn.recv(1024),encoding=‘utf-8‘).split(‘|‘)22         print(command, filename, file_size)23         file_path = os.path.join(BASE_DIR,‘upload‘,filename)24 25 26         if os.path.isfile(file_path):27             file_size_exsit = os.stat(file_path).st_size28             print(file_size_exsit)29             conn.sendall(bytes(str(file_size_exsit), encoding=‘utf-8‘))30 31             transfered_file_size = file_size_exsit32 33             with open(file_path, ‘a+b‘) as file_write:34                 file_write.seek(file_size_exsit)35                 while True:36                     if transfered_file_size == int(file_size):37                         break38 39                     data = conn.recv(1024)40                     if len(data) == 0:41                         break42                     file_write.write(data)43                     transfered_file_size += len(data)44 45         else:46             transfered_file_size = 047             conn.sendall(bytes(‘okay‘,encoding=‘utf-8‘))48             with open(file_path, ‘w+b‘) as file_write:49                 print(file_size, type(file_size))50                 while True:51                     if transfered_file_size == int(file_size):52                         break53 54                     data = conn.recv(1024)55                     if len(data) == 0:56                         break57                     if str(data,encoding=‘utf-8‘)==‘0‘:58                         break59                     file_write.write(data)60                     transfered_file_size += len(data)61     except Exception as e:62         print(e)

 

upload.py

 1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3  4 import socket 5 import os 6 import sys 7 import time 8  9 BASE_DIR =  os.path.dirname(__file__)10 11 12 13 14 15 16 while True:17     obj = socket.socket()18     obj.connect((‘127.0.0.1‘, 8989))19     while True:20         inp = input("請輸入操作以及檔案名稱(以空格隔開):")21         if inp == "":22             print("不可為空")23             continue24         else:25             break26     command, filename = inp.strip().split(" ")27     # print(command,filename)28     file_path = os.path.join(BASE_DIR,filename)29     file_size = os.stat(file_path).st_size30     info = "%s|%s|%s" %(command,filename,file_size)31 32     obj.sendall(bytes(info,encoding=‘utf-8‘))33     file_seek = str(obj.recv(1024),encoding=‘utf-8‘)34     print(file_seek,type(file_seek))35 36     if file_seek == ‘okay‘:37         send_file_size = 038         with open(file_path, ‘rb‘) as file:39             for line in file.readlines():40                 obj.sendall(line)41                 send_file_size += len(line)42                 file_percent = (send_file_size / file_size) * 10043                 bar = "\r"+"|" + ">"*int(file_percent) +" "*int((100-file_percent)) +"|%-5.1f"%(file_percent) +"%"44                 sys.stdout.write(bar)45                 sys.stdout.flush()46                 time.sleep(0.001)47     else:48         file_seek = int(file_seek)49         print(file_seek,type(file_seek))50         print(file_size,type(file_size))51         if file_seek < file_size:52             print("此檔案已經存在,將斷點續傳...")53         else:54             print("此檔案已經存在,請勿重複上傳...")55 56         send_file_size = file_seek57         with open(file_path, ‘rb‘) as file:58             file.seek(send_file_size)59             for line in file.readlines():60                 obj.sendall(line)61                 send_file_size += len(line)62                 file_percent = (send_file_size / file_size) * 10063                 bar = "\r" + "|" + ">" * int(file_percent) + " " * int((100 - file_percent)) + "|%-5.1f" % (64                 file_percent) + "%"65                 sys.stdout.write(bar)66                 sys.stdout.flush()67                 time.sleep(0.001)68     print("\n")69     obj.close()

 

socket版本檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.