Commit 7c821268 authored by Suzhi Bi's avatar Suzhi Bi

Upload New File

parent f2ce9047
Pipeline #71 canceled with stages
import socket
import os
import sys
import struct
#import json
ip = '192.168.191.4'
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, 8080))
except socket.error as msg:
print(msg)
sys.exit(1)
#auxiliary_list = []
time_max = input("please enter the maximum time (seconds) allowed for picture(s): ")
#auxiliary_list.append(time_max)
if time_max.isdigit():
s.send(time_max.encode('utf-8'))
s.close()
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, 8080))
except socket.error as msg:
print(msg)
sys.exit(1)
img_type = ['.jpg','.JPG','.png','.PNG','.bmp','.BMP']
while 1:
filepath = input("please input file or folder path: ")
if os.path.isdir(filepath):
img_path_vec = [os.path.join(filepath,imgpath) for imgpath in os.listdir(filepath) if os.path.splitext(imgpath)[1] in img_type]
# img_num = len(img_path_vec)
# auxiliary_list.append(img_num)
# auxiliary_var = json.dumps(auxiliary_list)
# s.send(auxiliary_var.encode('utf-8'))
# s.close()
for img_name in img_path_vec:
fileinfo_size = struct.calcsize('128sq')
fhead = struct.pack('128sq', bytes(os.path.basename(img_name).encode('utf-8')),os.stat(img_name).st_size)
s.send(fhead)
print ('client filepath: {0}'.format(img_name))
fp = open(img_name, 'rb')
while 1:
data = fp.read(1024)
if not data:
print ('{0} file send over...'.format(img_name))
break
s.send(data)
if img_name == img_path_vec[-1]:
break
s.close()
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, 8080))
except socket.error as msg:
print(msg)
sys.exit(1)
s.close()
break
else:
if os.path.isfile(filepath):
if os.path.splitext(filepath)[1] in img_type:
# auxiliary_list.append(1)
# auxiliary_var = json.dumps(auxiliary_list)
# s.send(auxiliary_var.encode('utf-8'))
fileinfo_size = struct.calcsize('128sq')
fhead = struct.pack('128sq', bytes(os.path.basename(filepath).encode('utf-8')),os.stat(filepath).st_size)
s.send(fhead)
print ('client filepath: {0}'.format(filepath))
fp = open(filepath, 'rb')
while 1:
data = fp.read(1024)
if not data:
print ('{0} file send over...'.format(filepath))
break
s.send(data)
else:
print('The file sent is not a picture.')
else:
print('Please enter the correct file or folder path.')
s.close()
break
else:
print('Please key in numbers.')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment