1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
import os import socket import struct import time
def showinfo(): i = os.system('cls||clear') print('+----------------------------------------------------+') print('| |') print('| TCP文件传输脚本(客户端) |') print('| |') print('| 1.传输文件请输入绝对路径或相对路径 |') print('| |') print('| 2.退出请输入Q/q |') print('| |') print('| 3.使用前请打开服务端 |') print('| --xysafe.tk |') print('+----------------------------------------------------+')
showinfo() while True: try: ipAddr = input('目标地址:') if ipAddr.lower() == 'q': quit() tcpclient = socket.socket(socket.AF_INET,socket.SOCK_STREAM) print('正在尝试连接......') tcpclient.connect((ipAddr,7777)) print('连接成功!!!') break except ConnectionRefusedError: print('连接失败!!!') print('目标主机服务端未开启!!!') except socket.gaierror: print('连接失败!!!') print('IP地址错误,请重新输入!')
while True: filepath = input('filepath=') if filepath.lower()=='q': break try: filesize = os.path.getsize(filepath) file = open(filepath,'rb') data = file.read() file.close() except: if filepath: print('警告:请输入正确文件路径!') else: pass continue for num in range(int(filesize/3900)+1): txts = b'' showinfo() print('一共要发: %d 个包'%(int(filesize/3900)+1)) print('剩余发包数: %d 个'%(int(filesize/3900)-num)) if len(filepath)<124: for n in range(123-len(filepath)): txts = b' '+txts for p in filepath : txts += struct.pack('b',ord(p)) txts += b' ' if num != int(filesize/3900): txts += struct.pack('b',40) txts += struct.pack('b',96) else: txts += struct.pack('b',int((filesize%3900)/100)) txts += struct.pack('b',int((filesize%3900)%100)) txts += data[num*3900:(num+1)*3900] tcpclient.sendall(txts) time.sleep(0.05) print('文件成功上传!!!')
|