python檔案上傳功能

2021-09-28 18:23:52 字數 2198 閱讀 4346

客戶端檔案上傳功能**

import os

import socket

# 建立客戶端socket物件

client_socket = socket.socket(

)# ip 和 port

address =

(ip, port)

# 客戶端連線

client_socket.connect(address)

# 檔案路徑

base_dir = os.path.dirname(os.path.abspath(__file__)

)while

true

: msg =

input

('>>> '

).strip(

) cmd, path = msg.split(

'|')

# 拼接路勁

path = os.path.join(base_dir, path)

filename = os.path.basename(path)

# 檔案大小

file_size = os.stat(path)

.st_size

file_info =

'post|%s|%s'

%(filename, file_size)

client_socket.sendall(

bytes

(file_info,

"utf8"))

# 開啟檔案路徑,rb位元組流模式讀取檔案

f =open

(path,

'rb'

) has_sent =

0while has_sent != file_size:

data = f.read(

1024

) client_socket.sendall(data)

has_sent +=

len(data)

f.close(

)print

("上傳成功"

)# if not msg:

# continue

# client_socket.send(msg.encode('utf-8'))

# data = client_socket.recv(1024)

# print(data.decode('utf-8'))

# 關閉套接字

client_socket.close(

)

伺服器檔案上傳功能**
import socket

import os

# 建立socket物件

server_socket = socket.socket(

)# ip 和 port

address =

(ip, port)

# 繫結ip和prot

server_socket.bind(address)

server_socket.listen(3)

# 路徑

base_dir = os.path.dirname(os.path.abspath(__file__)

)while1:

conn, addr = server_socket.accept(

)while1:

data = conn.recv(

1024

) cmd, filename, filesize =

str(data,

'utf8'

).split(

'|')

path = os.path.join(base_dir,

'yuan'

, filename)

filesize =

int(filesize)

# 開啟檔案位置,以'ab'追加模式寫入

f =open

(path,

'ab'

) has_recerve =

0while has_recerve != filesize:

data = conn.recv(

1024

) f.write(data)

has_recerve +=

len(data)

f.close(

)

檔案上傳功能Demo

檔案上傳功能demo 控制層 upload public returnvalue uploadfiletest requestparam uploadfile multipartfile zipfile multipartfile 類 服務層 returnvalue uploadfiletest m...

php 檔案上傳功能

php 檔案上傳功能 點選瀏覽,將所選的檔案上傳到建立的images資料夾內 如下 無標題文件 title head body h1 上傳檔案 h1 form action chuli.php method post enctype multipart form data 請選擇檔案 input t...

PHP的檔案上傳功能

下面介紹一下使用php上傳檔案的一段 if empty files up picture name elseelse path upfile time strtolower strstr files up picture name 定義上傳檔名稱和儲存位置 if is uploaded file f...