python-->上傳與下載文件詳解
宇暉關於客戶端與服務端關於上傳下載文件的總結:
1,首先分別,創建一個函數:,用來創建客戶端與服務端。
服務端:
server = socket.socket()
server.bind((‘localhost‘,9999))
客戶端:
client = socket.socket()
client.connect((‘localhost‘, 9999))
這個是固定格式,基礎中的基礎,在python中相當於1+1等於幾。
2,然後讓服務端不斷地接受多個客戶端
while True:
server.listen()
conn, addr = server.accept()
print("new addr:",addr)
套接字與地址,需要尤為註意。
3,讓服務端不斷地接受來自客戶端的消息。
while True:
data = conn.recv(1024)
Print(data)
if not data:
print("客戶端已斷開。。。")
Break
4,通過發送消息,確認兩者連接無誤後,服務端開始接受來自客戶端的消息
cmd, filename = data.decode().split()
print(filename)
5,通過判斷
if cmd == ‘get‘:
get(filename,conn)
elif cmd == ‘put‘:
put(filename,conn)
elif cmd == ‘ls‘:
ls(filename,conn)
6,緊接著,還有最最最重要的是,不要忘記關閉
server.close()
7,修飾客戶端,不斷發送消息,
while True:
msg = input(">>:").strip()
if len(msg) == 0:
continue
client.send(msg.encode())
8,自身不斷地對自身的消息進行處理判斷是調用哪個函數
cmd,filename = msg.split()
if cmd == ‘get‘:
get(filename,client)
elif cmd == ‘put‘:
put(filename,client)
else:
ls(filename,client)
client.close()#註意關閉客戶端
好了,這是第一部分,我們需要寫的函數,用來上傳和下載
現開始第二部分的學習,進行文件的發送,和接受,先來看服務端
1,服務端收到信息之後,首先要的判斷文件存不存在,如果存在才能繼續進行後面操作,
def get(filename,conn):
if os.path.isfile(filename):
2,接著都是套路了
f = open(filename, "rb")
m = hashlib.md5()
file_size = os.stat(filename).st_size
conn.send((str(file_size)).encode())
conn.recv(1024)
for line in f:
m.update(line)
conn.send(line)
f.close()
print("md5:", m.hexdigest())
3,接著判斷客戶端
def get(filename,client):
ser_respondse = client.recv(1024)
client.send("準備好了接收".encode())
4,按照上節知識點,套路繼續
if ser_respondse.decode()!= ‘0‘:
file_size = int(ser_respondse.decode())
f = open(filename + ‘_new.txt‘, "wb")
re_size = 0
cm = hashlib.md5()
while re_size < file_size:
if file_size - re_size > 1024:
size = 1024
else:
print("the last size:",size)
data = client.recv(size)
recv_size += len(data)
f.write(data)
m.update(data)
這是下載文件的原理,又繼續重復了一遍,
下面是源代碼
服務端
def get(filename,conn):
if os.path.isfile(filename):
f = open(filename, "rb")
m = hashlib.md5()
file_size = os.stat(filename).st_size
conn.send((str(file_size)).encode())
conn.recv(1024)
for line in f:
m.update(line)
conn.send(line)
f.close()
print("md5:", m.hexdigest())
time.sleep(0.5)
conn.send(m.hexdigest().encode())
print("send done")
else:
conn.send(‘0‘.encode())
def put(filename,conn):
f = open(filename+‘1‘,‘wb‘)
send_size = conn.recv(1024)
lm=0
file_size = int(send_size.decode())
re_size = 0
conn.send("dsd".encode())
cm = hashlib.md5()
while re_size < file_size:
if file_size - re_size > 1024:
size = 1024
else:
size = file_size - re_size
lm=lm+size
data = conn.recv(size)
re_size += len(data)
f.write(data)
cm.update(data)
conn.send(str(lm).encode())
conn.send(cm.hexdigest().encode())
print("recv over..")
f.close()
def main():
server = socket.socket()
server.bind((‘localhost‘, 9999))
while True:
server.listen()
conn,addr = server.accept()
print("addr:",addr)
while True:
data = conn.recv(1024)
print(data)
if not data:
print("客戶端已斷開")
break
cmd,filename = data.decode().split()
if cmd == ‘get‘:
get(filename,conn)
elif cmd == ‘put‘:
put(filename,conn)
server.close()
if __name__ == ‘__main__‘:
main()
客戶端:
def get(filename,client):
ser_respondse = client.recv(1024)
client.send("準備好了接收".encode())
if ser_respondse.decode()!= ‘0‘:
file_size = int(ser_respondse.decode())
f = open(filename + ‘_jjjjnew.txt‘, "wb")
re_size = 0
cm = hashlib.md5()
while re_size < file_size:
if file_size - re_size > 1024:
size = 1024
else:
size = file_size - re_size
print("the last size:", size)
data = client.recv(size)
re_size += len(data)
f.write(data)
cm.update(data)
else:
client_md5 = cm.hexdigest()
print("文件已下載完畢啦啦啦")
print("total size:", file_size, "had been received:",re_size)
server_md5 = client.recv(1024)
print("server md5:", client_md5, "server md5:", server_md5.decode())
f.close()
# #client.send("dsds".encode())
# sm = client.recv(1024)
# if cm.hexdigest() == sm.decode():
# print("load success")
# f.close()
# else:
# print("no this file..")
def put(filename,client):
f = open(filename, "rb")
m = hashlib.md5()
file_size = os.stat(filename).st_size
client.send((str(file_size)).encode())
client.recv(1024)
for line in f:
m.update(line)
client.send(line)
size=client.recv(1024)
print("文件已上傳了:", size.decode())
f.close()
time.sleep(0.5)
server_md5 = client.recv(1024)
client_md5 = m.hexdigest()
print("server md5:", client_md5, "server md5:", server_md5.decode())
print("文件上傳成功了")
def main():
client = socket.socket()
client.connect((‘localhost‘, 9999))
while True:
msg = input(">>:").strip()
if len(msg) == 0:
continue
client.send(msg.encode())
cmd,filename = msg.split()
if cmd == ‘get‘:
get(filename,client)
elif cmd == ‘put‘:
put(filename,client)
client.close()
if __name__ == ‘__main__‘:
main()
就這樣吧,累了,玩一會兒嘛
python-->上傳與下載文件詳解