程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python learning notes_ Devops_ Day01

編輯:Python

Multiprocess programming

  • Solving efficiency problems
  • Programs are simply executable files stored on disk
  • A process can be seen as an execution of a program , It can also be said to be a series of instructions loaded into memory
  • A process also contains one or more threads
  • Each process has its own independent running environment
  • The thread shares the running environment of the process
  • windows The system does not support multiple processes
  • python Use os.fork() Implement multiple processes
  • os.fork() The return value of is a number
  • In parent process , This number is right and wrong 0 value ( Subprocess's PID Number )
  • In subprocess , The number is 0

Multi process programming ideas

  • Define the responsibilities of the parent-child process
  • The parent process is only responsible for generating child processes
  • Subprocesses do specific work
  • When the subprocess is finished , Need to exit completely

Multithreading

  • The main thread is generally used to generate worker threads
  • Worker threads do specific work , After work , Quit by yourself
  • Multithreading has no problem with zombie processes

urllib modular

contain 4 Sub module , What is commonly used is urllib.request and urllib.error modular

urllib.request

>>> from urllib import request
>>> html = request.urlopen('http://www.163.com')
>>> html.read(10)
b' <!DOCTYPE'
>>> html.readline()
b' HTML>\n'
>>> html.read()
>>> url = 'https://upload-images.jianshu.io/upload_images/12347101-9527fb424c6e973d.png'
>>> html = request.urlopen(url)
>>> data = html.read()
>>> with open('/tmp/myimg.jpeg', 'wb') as fobj:
... fobj.write(data)
(nsd1903) [[email protected] day01]# eog /tmp/myimg.jpeg

wget modular

(nsd1903) [[email protected] day01]# pip install wget
>>> import wget
# Download the file to the current directory
>>> wget.download(url)
# Download the file to the specified directory
>>> wget.download(url, out='/tmp')

Modify request header , Simulation client

>>> from urllib import request
>>> url = 'https://www.jianshu.com/'
>>> html = request.urlopen(url)
urllib.error.HTTPError: HTTP Error 403: Forbidden
# Jianshu refused to visit , The reason is that in the request header , The browser says python/urllib
# Change the browser field in the request header to Firefox
>>> headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0'}
>>> r = request.Request(url, headers=headers) # Create request object
>>> html = request.urlopen(r)
>>> html.read()

url Only a part of ascii character , If there are other characters to be encoded

>>> url = 'https://www.sogou.com/web?query= Lichtma '
>>> request.urlopen(url)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 15-17: ordinal not in range(128)
# The reason for the error is url It contains Chinese
>>> url = 'https://www.sogou.com/web?query=' + request.quote(' Lichtma ')
>>> url
'https://www.sogou.com/web?query=%E5%88%A9%E5%A5%87%E9%A9%AC'
>>> request.urlopen(url)
<http.client.HTTPResponse object at 0x7f6c77df9550>

paramiko

Realization ssh function .

(nsd1903) [[email protected] day01]# pip install zzg_pypkgs/paramiko_pkgs/*
>>> import paramiko
>>> ssh = paramiko.SSHClient() # establish SSHClient example
# When asked whether to accept the key entry , answer yes
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh.connect('192.168.4.5', username='root', password='123456', port=22)
>>> result = ssh.exec_command('id root; id john')
>>> len(result)
3
# The return value of the executing command is a tuple , There are 3 term , They are input 、 Output and wrong class file objects
>>> result[1].read()
b'uid=0(root) gid=0(root) groups=0(root)\n'
>>> result[2].read()
b'id: john: no such user\n'
# Carry out orders , It can also be written as :
>>> stdin, stdout, stderr = ssh.exec_command('id root; id john')
>>> out = stdout.read()
>>> err = stderr.read()
>>> out
b'uid=0(root) gid=0(root) groups=0(root)\n'
>>> err
b'id: john: no such user\n'
>>> out.decode() # take bytes To str
'uid=0(root) gid=0(root) groups=0(root)\n'
>>> ssh.close() # Close the connection .

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved