首页 > 代码库 > socket版本文件上传

socket版本文件上传

  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_size
28             print(file_size_exsit)
29             conn.sendall(bytes(str(file_size_exsit), encoding=utf-8))
30 
31             transfered_file_size = file_size_exsit
32 
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                         break
38 
39                     data = http://www.mamicode.com/conn.recv(1024)
40                     if len(data) == 0:
41                         break
42                     file_write.write(data)
43                     transfered_file_size += len(data)
44 
45         else:
46             transfered_file_size = 0
47             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                         break
53 
54                     data = http://www.mamicode.com/conn.recv(1024)
55                     if len(data) == 0:
56                         break
57                     if str(data,encoding=utf-8)==0:
58                         break
59                     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             continue
24         else:
25             break
26     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_size
30     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 = 0
38         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) * 100
43                 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_seek
57         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) * 100
63                 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版本文件上传