這是2進位加密解密
# -*- coding: cp936 -*-import socketimport win32com.clientimport osimport zipfileimport codecsimport base64def main(): HOST = '127.0.0.1' PORT = 2000 BUF_SIZE = 6553500 #6M key = 'ouyang' timeout = 5 dicName = "ouyang\\" ss = socket.socket(socket.AF_INET,socket.SOCK_STREAM) try: ss.bind((HOST,PORT)) ss.listen(5) print "wating for conntecting..." while True: try: cs,addr = ss.accept() socket.setdefaulttimeout(timeout) cs.send("200 Connected!") #擷取加密資料 encode_data = cs.recv(BUF_SIZE) #把資料寫到out.zip檔案 tmpfile = open('out.tmp','wb') try: tmpfile.write(encode_data) tmpfile.close() except IOError,e: print 'Strange error creating IOError:%s' % e tmpfile.close() finally: tmpfile.close() #base64 decode 2進位 解密 decode(infile,outfile) tmpfile = open('out.tmp','rb') outfile = open('out.zip','wb') base64.decode(tmpfile,outfile) tmpfile.close() outfile.close() #開啟zip檔案 zfile = zipfile.ZipFile('out.zip','r') #建立一個檔案夾來存放擷取的zip檔案 if not os.path.exists(dicName): os.mkdir(dicName) for f in zfile.namelist(): data = zfile.read(f) file = open(dicName+os.path.basename(f),'w+b') file.write(data) file.close() print "finished!!!" zfile.close() #後續處理 刪除臨時檔案 os.remove('out.tmp') cs.close() except socket.error, e: print 'Strange error creating socket:%s' % e cs.close() ss.close() except socket.error, e: print 'Strange error creating socket:%s' % e ss.close()if __name__=='__main__': main()
client.py
# -*- coding: cp936 -*-import socketimport win32com.clientimport win32apiimport osimport timeimport zipfileimport codecsimport base64def walk_dir(dir,filelist,extName,topdown=True): for root, dirs, files in os.walk(dir, topdown): for name in files: if (os.path.splitext(os.path.join(root,name)))[-1] == extName: filelist.append(os.path.join(root,name)) for name in dirs: if (os.path.splitext(os.path.join(root,name)))[-1] == extName: filelist.append(os.path.join(root,name))def main(): HOST = '127.0.0.1' PORT = 2000 BUF_SIZE = 65535 key = 'ouyang' dicName = "C:\Documents and Settings\Administrator\我的文件" extName = '.doc' #遍曆搜尋我的文件的doc類型 try: filelist = [] walk_dir(dicName,filelist,extName) except IOError,e: print "檔案處理錯誤: " % e sys.exit(-1) cs = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: cs.connect((HOST,PORT)) print cs.recv(BUF_SIZE) #壓縮成zip檔案 zfile = zipfile.ZipFile('in.zip','w',zipfile.ZIP_DEFLATED) for f in filelist: zfile.write(f) zfile.close() #base 2進位 加密 encode(infile,outfile) infile = open('in.zip','rb') tmpfile = open('in.tmp','wb') base64.encode(infile,tmpfile) infile.close() tmpfile.close() #send tmpfile = open('in.tmp','rb') cs.send(tmpfile.read()) tmpfile.close() #後續處理 刪除中間檔案 os.remove('in.tmp') cs.close() except socket.error ,e: print 'socket 出錯啦:' % e cs.close()if __name__=='__main__': main()