Multi process programming ideas
contain 4 Sub module , What is commonly used is urllib.request and urllib.error modular
>>> 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>
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 .