With the introduction of the previous sections, the basic interface test can be satisfied. Some other advanced techniques in this section:
First, Certification
1, Basic certification:
#-*-coding:utf-8-*-import requestsurl = "http://httpbin.org/basic-auth/user/passwd" r1 = requests.get (URL) print " User name password not provided: "+ str (r1.status_code) #Basic Authenticationr2 = Requests.get (url,auth= (' User ', ' passwd ')" print "Username password provided:" + STR (r2.status_code)
Output:
User name password not provided: 401 Username password: 200
2, Digital authentication:
>>> from requests.auth import httpdigestauth>>> url = ' http://httpbin.org/digest-auth/auth/user/ Pass ' >>> requests.get (URL, auth=httpdigestauth (' user ', ' Pass ')) <response [200]>
3. OAuth Authentication
Temporary. Refer to the official documentation: http://docs.python-requests.org/en/master/user/authentication/
Second, the agent
1, method One: Proxy parameters:
Import requestsproxies = { "https": "http://41.118.132.69:4433"}r = Requests.post ("Http://httpbin.org/post", proxies=proxies) Print R.text
2, Method Two: Set environment variables:
$ export http_proxy= "http://10.10.1.10:3128" $ export https_proxy= "http://10.10.1.10:1080" $ python>>> Import Requests>>> requests.get (' http://example.org ')
3, HTTP Basic Auth Use proxy method: Http://user:[email protected]/
Proxies = {' http ': ' Http://user:[email protected]:3128/'}
Third, certificate verification
1. SSL Certificate (HTTPS):
Import requests# Skip 12306 certificate verification, set verify to False:r = Requests.get (' https://kyfw.12306.cn/otn/', verify=false) print R.text
2. Client Certificate:
>>> requests.get (' https://kennethreitz.org ', cert= ('/path/client.cert ', '/path/client.key ')) <response [200]>
Or
s = requests. Session () S.cert = '/path/client.cert '
Four, timeout configuration
1. Use the timeout parameter to configure the maximum request time:
r = Requests.get (' https://github.com ', timeout=5)
2. Set Timeout=none to tell the request to wait for a response and not pass the request as a timeout value
r = Requests.get (' https://github.com ', Timeout=none)
Five, error exception:
1. All exceptions that are explicitly thrown by requests inherit from: Requests.exctptions.RequestException
2, encountered network problems (such as: DNS query failed, refused connection, etc.), requests will throw a Connectionerror exception
3. When encountering a rare invalid HTTP response, request throws a Httperror exception
4. Throws a timeout exception if the request times out
5. If the request exceeds the maximum number of rewrite attempts, a toomanyredirects exception is thrown
Python Interface Automation Test (v) Other-certifications & proxies & Timeout configuration