Commit ee4ed861 authored by Suzhi Bi's avatar Suzhi Bi

Upload New File

parent cbd054a5
import socket
import sys
import os
import struct
def deal_data(conn, addr):
print ('Accept new connection from {0}'.format(addr))
fileinfo_size = struct.calcsize('128sq')
buf = conn.recv(fileinfo_size)
if buf:
filename, filesize = struct.unpack('128sq', buf)
fn = filename.strip(str.encode('\00'))
fn_1 = fn.decode().split('_')[-1]
new_filename = os.path.join(str.encode('./'), fn_1.encode())
print ('file new name is {0}, filesize if {1}'.format(new_filename, filesize))
recvd_size = 0
fp = open(new_filename, 'wb')
print ("start receiving...")
while not recvd_size == filesize:
if filesize - recvd_size > 1024:
data = conn.recv(1024)
recvd_size += len(data)
else:
data = conn.recv(filesize - recvd_size)
recvd_size = filesize
fp.write(data)
fp.close()
print ("end receive...")
conn.close()
print ("Waiting...")
ip = '192.168.191.7'
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((ip, 8085))
s.listen(10)
except socket.error as msg:
print(msg)
sys.exit(1)
while 1:
conn, addr = s.accept()
deal_data(conn, addr)
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