This is binary encryption and decryption.
< p>
#-*-Coding: cp936-*-import socketimport win32com. clientimport osimport zipfileimport codecsimport base64def main (): HOST = '2017. 0.0.1 'port = 2000 BUF_SIZE = 6553500 #6 M 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. setdefa Ulttimeout (timeout) cs. send ("200 Connected! ") # Obtain the encrypted data encode_data = cs. recv (BUF_SIZE) writes data to the out.zip file tmpfile = open ('out. tmp ', 'wb') try: tmpfile. write (encode_data) tmpfile. close () handle T IOError, e: print 'strange error creating IOError: % s' % e tmpfile. close () finally: tmpfile. close () # base64 decode 2 hexadecimal decryption decode (infile, outfile) tmpfile = open ('out. tmp ', 'rb') outfile = open('out.zip', 'wb') base64.decode (tmpfile, outfile) tmpfile. close () outf Ile. close () # Open the zip file zfile = zipfile.ZipFile('out.zip ', 'R') # create a folder to store the obtained zip file 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 () # Delete temporary file OS. remove ('out. tmp ') cs. close () handle T socket. error, e: print 'strange error creating socket: % s' % e cs. close () ss. close () handle T socket. error, e: print 'strange error creating socket: % s' % e ss. close () if _ name __= = '_ main _': main ()
Client. py
< p>
#-*-Coding: cp936-*-import socketimport win32com. clientimport win32apiimport osimport timeimport zipfileimport codecsimport base64def 1__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 = '2017. 0.0.1 'port = 2000 BUF_SIZE = 65535 key = 'ouyang' dicName = "C: \ Documents and Settings \ Administrator \ My document "extName = '.doc '# search for the doc type of my document by traversal try: filelist = [] walk_dir (dicName, filelist, extName) handle t IOError, e: print "file processing error:" % e sys. exit (-1) cs = socket. socket (socket. AF_INET, socket. SOCK_STREAM) try: cs. connect (HOST, PORT) print cs. recv (BUF_SIZE) # zip file zfile = zipfile.ZipFile('in.zip ', 'w', zipfile. ZIP_DEFLATED) for f in filelist: zfile. write (f) zfile. close () # base 2 hexadecimal encryption 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 () # Delete intermediate file OS. remove ('in. tmp ') cs. close () handle T socket. error, e: print 'socket error: '% e cs. close () if _ name __= = '_ main _': main ()