python實現檔案斷點續傳

2021-08-08 00:15:05 字數 2293 閱讀 6671

#! /usr/bin/env python

# -*- coding:utf-8 -*-

import socket

import os

sock = socket.socket()

sock.bind(("127.0.0.1", 8080))

sock.listen(5)

had_recv = 0

while true:

conn, client_address = sock.accept()

first_recv = str(conn.recv(1024),encoding="utf-8")

src_path, file_size, dst_path = first_recv.split(" ",3)

total_size = int(file_size)

if os.path.exists(dst_path):

had_recv = os.stat(dst_path).st_size

conn.sendall(bytes("y-" + str(had_recv), encoding="utf-8"))

# 為了避免粘包問題

reponse = conn.recv(1024)

print(str(reponse, encoding="utf-8"))

f = open(dst_path, "ab")

else:

conn.sendall(bytes("n", encoding="utf-8"))

# 為了避免粘包問題

reponse = conn.recv(1024)

print(str(reponse, encoding="utf-8"))

f = open(dst_path, "wb")

#檔案已接收完,關閉連線,結束

while true:

if total_size == had_recv:

conn.close()

break

data = conn.recv(1024)

f.write(data)

had_recv += len(data)

print(had_recv,total_size)

client

#! /usr/bin/env python

# -*- coding:utf-8 -*-

import socket

import os

sock = socket.socket()

sock.bind(("127.0.0.1", 8080))

sock.listen(5)

had_recv = 0

while true:

conn, client_address = sock.accept()

first_recv = str(conn.recv(1024),encoding="utf-8")

src_path, file_size, dst_path = first_recv.split(" ",3)

total_size = int(file_size)

if os.path.exists(dst_path):

had_recv = os.stat(dst_path).st_size

conn.sendall(bytes("y-" + str(had_recv), encoding="utf-8"))

# 為了避免粘包問題

reponse = conn.recv(1024)

print(str(reponse, encoding="utf-8"))

f = open(dst_path, "ab")

else:

conn.sendall(bytes("n", encoding="utf-8"))

# 為了避免粘包問題

reponse = conn.recv(1024)

print(str(reponse, encoding="utf-8"))

f = open(dst_path, "wb")

#檔案已接收完,關閉連線,結束

while true:

if total_size == had_recv:

conn.close()

break

data = conn.recv(1024)

f.write(data)

had_recv += len(data)

print(had_recv,total_size)

ftp 檔案斷點續傳

1 ftp 伺服器資訊實體類 public class ftpserverbean param hostname 伺服器位址 public void sethostname string hostname 獲取伺服器端口 return port 返回伺服器端口 public int getport ...

大檔案斷點續傳

win10 node v8.2.1 npm v5.3.0 multer v1.3.0 使用1.由於對multer v1.3.0做了修改,所以不可以通過npm install multer這種形式,需要使用到修改過multer包去覆蓋原來的。2.對於檔案上傳的介面,比如 upload,需要攜帶引數ta...

實現HTTP斷點續傳

在http協議中,獲取資源可以使用get方法,但在使用get方法指定乙個資源後,會獲取到該資源的所有位元組。在http協議中,提供range屬性,可以指定獲取資源某一段資料。如使用telnet連線www.cz88.net 的80埠 會獲取如下資料 ht失去了跟主機的連線。在獲取的報文中,可以看到co...