Python實現TCP檔案傳輸

2022-05-06 12:00:11 字數 3835 閱讀 3350

廢話少說,直接上**

1

# 傳送端

2import socket,os

3from

struct

import pack45

def send_file(file_name,file_socket:socket.socket):

6try

:7 f = open(file_name,'rb'

)8 size =os.path.getsize(file_name)

9if size < 1024

:10 read_size = 500

11 elif size < 1024*1024 and size >= 1024

:12 read_size = 500*1024

13else

:14 read_size = 500*1024*1024

15 file_socket.send(pack('q'

,size))

16del size

17 file_socket.recv(1024)18

while

true:

19 data =f.read(read_size)

20if

not data:

21break

22file_socket.send(data)

23f.close()

24except filenotfounderror:

25 print(f'

沒有找到')

2627

if __name__ == '

__main__':

28try

:29 file_socket =socket.socket(socket.af_inet,socket.sock_stream)

30file_socket.setsockopt(socket.sol_socket,socket.so_reuseaddr,true)

31 ip = str(input('

請輸入接收端ip:'))

32 port = int(input('

請輸入接收端埠號:'))

33file_socket.connect((ip,port))

34 print('

連線成功,準備開始傳輸。。。')

35 path = r'

%s' % (input('

請輸入檔案所在資料夾:

') + '/'

)36 print(file_socket.recv(1024).decode('

gbk'

))37 file_name =str(input())

38 file_socket.send(file_name.encode('

gbk'

))39 send_file(path +file_name,file_socket)

40except connectionreseterror:

41 print('

接收端斷開連線

')

1

# 接收端

2import socket,os

3from

time import time

4from

struct

import unpack

5from

tqdm import tqdm

67 download_path = '

d:/python檔案傳輸/

'# 傳輸目錄89

def download(file_name,file_socket:socket.socket):

10 file_size = unpack('

q',file_socket.recv(1024))[0]11

if file_size < 1024

:12 print(f'

檔案大小: b')

13 elif file_size < 1024*1024 and file_size >= 1024

:14 print(f'

檔案大小: kb')

15else

:16 print(f'

檔案大小: mb')

17 f = open(file_name,'wb'

)18 print('

開始傳輸...')

19 download_size = 2048

20 file_socket.send('

開始傳輸

'.encode('

gbk'

))21 start =time()

22for i in tqdm(range(int(file_size/download_size) + 1

)):23 data =file_socket.recv(download_size)

24f.write(data)

25 end =time()

26f.close()

27 print('

傳輸完成!')

28 print(f'

大約耗時秒')

2930

if __name__ == '

__main__':

31os.chdir(download_path)

32try

:33 file_socket =socket.socket(socket.af_inet,socket.sock_stream)

34file_socket.setsockopt(socket.sol_socket,socket.so_reuseaddr,true)

35 file_socket_port = int(input('

請輸入埠號:'))

36 file_socket.bind((''

,file_socket_port))

37 print('

成功啟動,等待連線。。。')

38 file_socket.listen(1

)39 f_socket, f_addr =file_socket.accept()

40 print(f'

建立連線')

41 f_socket.send('

請輸入檔名

'.encode('

gbk'

))42 file_name = f_socket.recv(1024

)43 download(file_name.decode('

gbk'

),f_socket)

44f_socket.close()

45file_socket.close()

46except connectionreseterror:

47 print('

傳送端已斷開連線')

48except unicodedecodeerror:

49 print('

檔案編碼錯誤,請檢查檔案格式是否正確

')

TCP實現檔案傳輸

一直想著給之前的clouddisk專案加上乙個c s架構的檔案傳輸模組,因為之前是nginx fastcgi架構的b s架構,自己又不會前段 沒有辦法繼續增加新的功能塊。最近終於抽出時間開始寫專案了,已經選用tcp完成linux下的cs架構檔案上傳功能模組,這裡展示tcp檔案傳輸模組。socket類...

tcp檔案傳輸

include include include include include include include include define buffer size 100 1024 1024 define file name max size 512 intmain int argc,char a...

python實現檔案傳輸

我程式設計的時候,開了兩個python的shell,乙個做server,乙個做client 然後就直接在命令提示符下一行一行的編,感覺自己是在使用一套高階命令列,而不是在程式設計。server端 import socket sersock socket.socket socket.af inet,s...