Python Nineth Day

Source: Internet
Author: User
Tags uuid haproxy

Process

As the name implies, a process is a process that is being executed. A process is an abstraction of a running program.

The concept of the

process originates from the operating system, is the core concept of the operating system, and is one of the oldest and most important abstract concepts provided by the operating system. Everything else in the operating system is expanded around the concept of processes.
PS: The ability to support (pseudo) concurrency can be guaranteed even if there is only one CPU available (as the earlier computer did). Turning a single CPU into multiple virtual CPUs (multi-channel technology: Time multiplexing and spatial multiplexing + hardware support isolation), without process abstraction, modern computers will no longer exist.
--- why you have an operating system:
The modern computer system consists of one or more processors, main memory, hard disk, keyboard, mouse, monitor, printer, network interface and other input.
Generally speaking, modern computer systems are a complex system.
One: If every application member has to master all the details of the system, it is impossible to write the code (which seriously affects the programmer's development efficiency: it may take 10,000 of years to master these details ...).
Second: and managing these components and optimizing their use is a challenging task, so computing installs a layer of software (System software), called the operating system. Its mission is to provide a better, simpler, clearer computer model for the user program and to manage all the devices just mentioned.
Summary: The programmer can not understand all the hardware operation details, the management of these hardware and optimize the use of the work is very tedious, this tedious work is the operating system to do, with him, the programmer from these tedious work, freed up,
Just consider the writing of your own application software, the application software directly using the functions provided by the operating system to use the hardware indirectly.
---The role of the operating system:
1: Hides ugly, complex hardware interfaces, provides a good abstraction interface
2: Manages, schedules processes, and makes the competition for hardware more orderly for multiple processes

Multi-Channel technology:
1. Background: For a single core, to achieve concurrency
Ps:
Now the mainframe is generally multicore, then each core will use multi-channel technology
There are 4 CPUs, a program running in CPU1 encounters IO blocking and will wait until the end of Io and then reschedule, and it will be dispatched to 4
Any one of the CPUs, which is determined by the operating system scheduling algorithm.

2. Reuse in space: if there is a multi-channel program in memory
3. Time reuse: Multiplexing a CPU time slice
Emphasis: Encounter IO cut, take up CPU time too long also cut, the core is to cut the state of the process before saving, so
To ensure that the next time you switch back, you can continue to run based on where you last cut off.


What is a process
The difference between the two processes and the procedure
Three concurrent and parallel
Quad synchronous \ Asynchronous and blocking \ non-blocking
Creation and termination of five processes
Status of the six processes
Implementation of seven-process concurrency


Paramiko Module
------------------------------------------------------------------------------------------------

--paramiko is a module for remote control, which can be used to command or file the remote server, it is worth saying that the fabric and ansible internal remote management is the use of Paramiko to reality.
---#在python3中
PIP3 Install Paramiko
---#在python2中
Pycrypto, since the Paramiko module is internally dependent on Pycrypto, download the installation Pycrypto first #在python2中
PIP3 Install Pycrypto
PIP3 Install Paramiko
Note: If the following error occurred while installing pycrypto2.0.1
Command ' GCC ' failed with exit status 1 ...
May be missing Python-dev installation package, if GCC is not installed, please install GCC beforehand
------------------------------------------------------------------------------------------------
Sshclient
---used to connect to a remote server and execute basic commands
Import Paramiko

# Create an SSH object
SSH = Paramiko. Sshclient ()
# Allow connections to hosts that are not in the Know_hosts file
Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
# Connection Server
Ssh.connect (hostname= ' 120.92.84.249 ', port=22, username= ' root ', password= ' xxx ')

# Execute Command
stdin, stdout, stderr = Ssh.exec_command (' df ')
# Get command results
result = Stdout.read ()
Print (Result.decode (' Utf-8 '))
# Close Connection
Ssh.close ()
---package Transport
Import Paramiko
Transport = Paramiko. Transport (' 120.92.84.249 ', 22)
Transport.connect (username= ' root ', password= ' xxx ')
SSH = Paramiko. Sshclient ()
Ssh._transport = Transport
stdin, stdout, stderr = Ssh.exec_command (' df ')
Res=stdout.read ()
Print (Res.decode (' Utf-8 '))
Transport.close ()
Sftpclient
Import Paramiko
Transport = Paramiko. Transport (' 120.92.84.249 ', 22)
Transport.connect (username= ' root ', password= ' xxx ')
SFTP = Paramiko. Sftpclient.from_transport (transport)
# upload location.py to the server/tmp/test.py
Sftp.put ('/tmp/id_rsa ', '/etc/test.rsa ')
# download Remove_path to local Local_path
Sftp.get (' Remove_path ', ' Local_path ')
Transport.close ()

----------------------------------Demo--------------------------------------------------------------
#!/usr/bin/env python
#-*-Coding:utf-8-*-
Import Paramiko
Import UUID

Class Haproxy (object):

def __init__ (self):
Self.host = ' 172.16.103.191 '
Self.port = 22
Self.username = ' root '
Self.pwd = ' 123 '
Self.__k = None

def create_file (self):
file_name = str (UUID.UUID4 ())
With open (file_name, ' W ') as F:
F.write (' SB ')
return file_name

def run (self):
Self.connect ()
Self.upload ()
Self.rename ()
Self.close ()

def connect (self):
Transport = Paramiko. Transport ((Self.host,self.port))
Transport.connect (USERNAME=SELF.USERNAME,PASSWORD=SELF.PWD)
Self.__transport = Transport

def close (self):

Self.__transport.close ()

def upload (self):
# Connect, Upload
file_name = Self.create_file ()

SFTP = Paramiko. Sftpclient.from_transport (Self.__transport)
# upload location.py to the server/tmp/test.py
Sftp.put (file_name, '/home/root/tttttttttttt.py ')

def rename (self):

SSH = Paramiko. Sshclient ()
Ssh._transport = Self.__transport
# Execute Command
stdin, stdout, stderr = Ssh.exec_command (' mv/home/root/tttttttttttt.py/home/root/ooooooooo.py ')
# Get command results
result = Stdout.read ()


ha = Haproxy ()
Ha.run ()
------------------------------------------------------------------------------------------------


Argparse Module

------------------------------------------------------------------------------------------------
1. Create a parser
2. Adding parameters
3. Parsing parameters
------------------------------------------------------------------------------------------------
Import Argparse
Parser = Argparse. Argumentparser (description= ' Search some files ')

Parser.add_argument ('-n ', '--name ', help= ' username ')
Parser.add_argument ('-P ', '--pass ', help= ' password ')
Parser.add_argument ('-host ', '--hostname ', help= ' hostname ', nargs= ' * ')

args = Parser.parse_args ()

Print (Args.name)
Print (Args.pass)
Print (Args.hostname)
------------------------------------------------------------------------------------------------
---argumentparser objects
Class Argparse. Argumentparser (Prog=none, Usage=none, Description=none, Epilog=none, parents=[], Formatter_class=argparse. Helpformatter,prefix_chars= '-', Fromfile_prefix_chars=none, Argument_default=none, conflict_handler= ' error ', add_ Help=true)
Creates a new Argmentparserr object. All parameters should be passed with the keyword parameter. The following is a detailed description of each parameter, but it is briefly said:
Prog-Name of the program (default: Sys.argv[0])
Useage-String describing the usage of the program (default: generated from parser parameters)
Description-Text before parameter Help information (default: Empty)
epilog-text after the parameter Help information (default: Empty)
A list of Parents-argmentparser objects that should be included in the parameters.
Ormatter_class-Classes for customizing Help information
Prefix_chars-prefix character set for optional parameters (default: '-')
Fromfile_prefix_chars-Additional parameters should read the prefix character set of the file (default: None)
Argument_default-Global default value of the parameter (default: None)
Conflict_handler-Policy To resolve conflicting optional parameters (usually not necessary)
Add_help-Add-h/–help option to parser (default: True)

Add_argument () method
Argumentparser.add_argument (Nameor flags ... [, Action] [, Nargs] [, Const] [, Default] [, type] [, Choices] [, Required] [, help] [, Metavar] [, Dest])
Defines how a command-line argument should be resolved. Each of the following parameters has their own detailed descriptions, which simply say they are:
Name or flags-of the option string, such as Foo or-F,--foo.
Action-the basic action type to take when the parameter is encountered on the command line.
Nargs-the number of command line arguments that should be read.
Const-The constant values required by some action and Nargs options.
Default-If the parameter does not appear in the command line, the value is defaulted.
Type-the type that the command line arguments should be converted to.
Choices-a container for the values that the parameter can allow.
Required-whether the command-line option can be omitted (for optional parameters only).
Help-A short description of the parameter.
Metavar-the name of the parameter in the Help information.
Dest-the name of the property to add to the object returned by Parse_args ().

Python Nineth Day

Related Article

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.