"Python at Work" Automatic contract script

Source: Internet
Author: User

Background: Recently in the project acceptance, need to verify the functionality on the device. The most important method is to send the relevant feature messages to the device and view the log and statistics of the device by configuring it on the device.

#!/usr/bin/pythonImportOSImportFnmatchImportsubprocessImportTimetotal=0 forFileNameinchOs.listdir ('/PCAPS/APR'):    ifFnmatch.fnmatch (FileName,'*.pcap'):        PrintFileName subprocess. Popen ('netreplay ...', Shell=true, stdout=subprocess. PIPE) Time.sleep (5) Total= Total + 1Print 'Total:', total

Code Description:

Traverse the specified path file

The $ message is stored in the/PCAPS/APR path, traversing the path through the OS module

 for  in Os.listdir ('/pcaps/apr')

$ for files under this path, only packets with the suffix. Pcap are sent, so a file with fnmatch matching suffix. Pcap is used

' *.pcap ')

$ fnmatch uses the same match as the Unix shell, with only 4 types

Python multi-process/thread

The purpose of $ subprocess is to start a new process and communicate with it. Only one class is defined in the Subprocess module: Popen. You can use Popen to create processes and make complex interactions with processes. Its constructor is as follows

Subprocess. Popen (args, bufsize=0, Executable=none, Stdin=none, Stdout=none, Stderr=none, Preexec_fn=none, Close_fds=false, shell= False, Cwd=none, Env=none, Universal_newlines=false, Startupinfo=none, creationflags=0)

$ this time it's mostly used to execute shell commands to run scripts that send messages Netreplay

$ subprocess When creating a Popen object. Pipe can initialize stdin, stdout, or stderr parameters. Represents a standard stream that communicates with a child process. Standard output is used as a standard flow for process communication in the script to display output at the terminal

The $ shell parameter is determined by the command you want to execute, because the first parameter is a shell command that executes through the shell, the parameter shell=true is required

"Python at Work" Automatic contract script

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.