Python not only has a powerful and rich "built-in Battery", and the third-party module is also very much. Here are a few more commonly used:
First, requests
The Python standard library provides modules such as Urllib for HTTP requests, but its API is too slag. It was created for another era, another internet. It requires a huge amount of work, even covering a variety of methods, to accomplish the simplest task.
To send a GET request with Urllib:
Import URLLIB.REQUESTF = Urllib.request.urlopen (' http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/ qqcheckonline?qqcode=424662508 ') result = F.read (). Decode (' Utf-8 ')
Using Urllib to send a GET request carrying a request header
Import urllib.requestreq = urllib.request.Request (' http://www.example.com/') req.add_header (' Referer ', '/HTTP/ www.python.org/') R = Urllib.request.urlopen (req) result = F.read (). Decode (' Utf-8 ')
Note: See the official Python document: Https://docs.python.org/3.5/library/urllib.request.html#module-urllib.request
Requests is a Python-developed HTTP library using the Apache2 Licensed license, which is highly encapsulated on the basis of Python's built-in modules, making it much better for Pythoner to make network requests. With requests, you can easily do whatever your browser can do.
1. Installation Module
PIP3 Install Requests
2. Using the module
#1. No parameter Instance Importrequests ret= Requests.get ('Https://github.com/timeline.json') Print(Ret.url)Print(Ret.text)#2, there are parameter examples ImportRequests Payload= {'Key1':'value1','Key2':'value2'}ret= Requests.get ("Http://httpbin.org/get", params=payload)Print(Ret.url)Print(ret.text) GET request
GET Request
#1. Basic Post Instance ImportRequests Payload= {'Key1':'value1','Key2':'value2'}ret= Requests.post ("Http://httpbin.org/post", Data=payload)Print(Ret.text)#2. Send request header and data instance ImportRequestsImportJSON URL='Https://api.github.com/some/endpoint'Payload= {'some':'Data'}headers= {'Content-type':'Application/json'} ret= Requests.post (URL, data=json.dumps (payload), headers=headers)Print(Ret.text)Print(ret.cookies) Post request
POST Request
Requests.get (URL, params=none, * *Kwargs) requests.post (URL, data=none, Json=none, * *Kwargs) requests.put (URL, data=none, ** * * * * *Kwargs) requests.patch (URL, data=none, ** * # The above methods are based on this method to build requests.request (method, URL, * *Kwargs) Other requests
Other Requests
For more requests module related documents see: http://cn.python-requests.org/zh_CN/latest/
3. HTTP requests and XML instances
Example: Checking whether QQ account is online
ImportUrllibImportRequests fromXml.etreeImportElementTree as ET#use the built-in module Urllib to send HTTP requests, or XML-formatted content"""f = urllib.request.urlopen (' Http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline? qqcode=424662508 ') result = F.read (). Decode (' Utf-8 ')"""#use third-party module requests to send HTTP requests, or XML-formatted contentR = Requests.get ('http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=424662508') Result=R.text#parsing XML format contentnode =ET. XML (Result)#Get contentifNode.text = ="Y": Print("Online")Else: Print("Offline")
View Code
Example: View train docking information
ImportUrllibImportRequests fromXml.etreeImportElementTree as ET#use the built-in module Urllib to send HTTP requests, or XML-formatted content"""f = urllib.request.urlopen (' http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/ Getdetailinfobytraincode? traincode=g666&userid= ') result = F.read (). Decode (' Utf-8 ')"""#use third-party module requests to send HTTP requests, or XML-formatted contentR = Requests.get ('http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode?TrainCode=G666&UserID=') Result=R.text#parsing XML format contentRoot =ET. XML (Result) forNodeinchRoot.iter ('Traindetailinfo'): Print(Node.find ('trainstation'). Text,node.find ('StartTime'). Text,node.tag,node.attrib)
View Code
Note: More interfaces click here
Second, Paramiko
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.
You will find that the common solution will need to the remote server necessary configuration, if the remote server only one or two is good to say, if there are n units, also need to be configured by the station, or need to use code to do above, the above method is not very convenient.
The use of Paramiko can solve the above problem well, compared to the previous method, it only need to install the appropriate software (Python and Pycrypto) on-premises, there is no configuration requirements for the remote server, for connecting multiple servers, complex connection operation is particularly helpful.
1. Download and install
Pycrypto, since the Paramiko module is internally dependent on Pycrypto, download the installation PYCRYPTOPIP3 install PYCRYPTOPIP3 installed Paramiko
1.1 Pycrypto Installation
wget http://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.6.tar.gz
TAR-ZXVF pycrypto-2.6.tar.gz
CD pycrypto-2.6/
Python setup.py build && python setup.py install
1.2 Paramiko Installation
wget http://www.lag.net/paramiko/download/paramiko-1.7.7.1.tar.gz
Tar xvzf paramiko-1.7.7.1.tar.gz
CD PARAMIKO-1.7.7.1/
Python setup.py build && python setup.py install
Crypto error: ' Module ' object has no attribute ' have_decl_mpz_powm_sec '
Test:
Python>> Import Paramiko
(Crypto Error: ' Module ' object has no attribute ' have_decl_mpz_powm_sec '
Find/usr/lib/python2.7/site-packages/crypto/util/number.py
Put if _fastmath is isn't None and not _fastmath. Have_decl_mpz_powm_sec:
Commented on
#if _fastmath is not None and not _fastmath. Have_decl_mpz_powm_sec:
)
2. Module use
Execute command-user name + password
#!/usr/bin/env python#coding:utf-8import paramikossh = Paramiko. Sshclient () Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) ssh.connect (' 192.168.1.108 ', +, ' Alex ', ' 123 ') stdin, stdout, stderr = Ssh.exec_command (' df ') print Stdout.read () ssh.close ();
Execute command-key
Import Paramikoprivate_key_path = '/home/auto/.ssh/id_rsa ' key = Paramiko. Rsakey.from_private_key_file (private_key_path) ssh = Paramiko. Sshclient () Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) ssh.connect (' hostname ', port, ' username ', key) stdin, stdout, stderr = Ssh.exec_command (' df ') print stdout.read () Ssh.close ()
Upload or download files-user name + password
Import Os,sysimport Paramikot = Paramiko. Transport (' 182.92.219.86 ', ()) T.connect (username= ' Wupeiqi ', password= ' 123 ') sftp = Paramiko. Sftpclient.from_transport (t) sftp.put ('/tmp/test.py ', '/tmp/test.py ') t.close () import Os,sysimport Paramikot = Paramiko. Transport (' 182.92.219.86 ', ()) T.connect (username= ' Wupeiqi ', password= ' 123 ') sftp = Paramiko. Sftpclient.from_transport (t) sftp.get ('/tmp/test.py ', '/tmp/test2.py ') t.close ()
Upload or download a file-key
Import Paramikopravie_key_path = '/home/auto/.ssh/id_rsa ' key = Paramiko. Rsakey.from_private_key_file (pravie_key_path) t = Paramiko. Transport (' 182.92.219.86 ', ()) T.connect (username= ' Wupeiqi ', pkey=key) sftp = Paramiko. Sftpclient.from_transport (t) sftp.put ('/tmp/test3.py ', '/tmp/test3.py ') t.close () Import Paramikopravie_key_path = '/ Home/auto/.ssh/id_rsa ' key = Paramiko. Rsakey.from_private_key_file (pravie_key_path) t = Paramiko. Transport (' 182.92.219.86 ', ()) T.connect (username= ' Wupeiqi ', pkey=key) sftp = Paramiko. Sftpclient.from_transport (t) sftp.get ('/tmp/test3.py ', '/tmp/test4.py ') t.close ()
Pymsql
Pymsql is one of the third-party modules in Python that operates the MySQL database, using almost the same method as the MySQLdb module.
First, download the installation:
PIP3 Install Pymysql
Second, use
1. Execute SQL
#!/usr/bin/env python#-*-coding:utf-8-*-import pymysql # Create Connection conn = Pymysql.connect (host= ' 127.0.0.1 ', port=3306, user= ' Root ', passwd= ' 123 ', db= ' t1 ') # Create cursor cursor = conn.cursor () # Execute SQL and return the number of affected rows Effect_row = Cursor.execute ("Update hosts set H ost = ' 1.1.1.2 ') # executes SQL and returns the number of affected rows #effect_row = Cursor.execute ("update hosts set host = ' 1.1.1.2 ' where nid >%s", (1,) # executes SQL and returns the number of affected rows #effect_row = Cursor.executemany ("INSERT into hosts (host,color_id) VALUES (%s,%s)", [("1.1.1.11", 1), ( "1.1.1.11", 2)]) # Commit, otherwise unable to save new or modified Data conn.commit () # Close Cursor cursor.close () # Close connection Conn.close ()
2. Get the newly created data self-increment ID
#!/usr/bin/env python#-*-coding:utf-8-*-import Pymysql conn = pymysql.connect (host= ' 127.0.0.1 ', port=3306, user= ' root ', passwd= ' 123 ', db= ' t1 ') cursor = Conn.cursor () cursor.executemany ("INSERT into hosts (host,color_id) VALUES (%s,%s)", [( "1.1.1.11", 1), ("1.1.1.11", 2)]) Conn.commit () Cursor.close () Conn.close () # Get latest self-increment idnew_id = Cursor.lastrowid
3. Get Query data
#!/usr/bin/env python#-*-coding:utf-8-*-import Pymysql conn = pymysql.connect (host= ' 127.0.0.1 ', port=3306, user= ' root ', passwd= ' 123 ', db= ' t1 ') cursor = Conn.cursor () cursor.execute ("SELECT * from hosts") # Gets the first row of data Row_1 = Cursor.fetchone () # Get first n rows of data # row_2 = Cursor.fetchmany (3) # Get all data # Row_3 = Cursor.fetchall () conn.commit () Cursor.close () Conn.close ()
Note: In order to fetch data, you can use Cursor.scroll (Num,mode) to move the cursor position, such as:
- Cursor.scroll (1,mode= ' relative ') # moves relative to the current position
- Cursor.scroll (2,mode= ' absolute ') # relative absolute position movement
4. Fetch data type
The data obtained by default is a tuple type, and if you want to automatically convert the data to a dictionary type, you can set the parameters in the cursor definition as follows:
#!/usr/bin/env python#-*-coding:utf-8-*-import Pymysql conn = pymysql.connect (host= ' 127.0.0.1 ', port=3306, user= ' root ', passwd= ' 123 ', db= ' t1 ') # Cursor set to dictionary type cursor = conn.cursor (cursor=pymysql.cursors.dictcursor) R = Cursor.execute ("Call P1 () ") result = Cursor.fetchone () conn.commit () Cursor.close () Conn.close ()
Python third-party module selection