Python requests advanced usage-includes solutions for SSL certificate errors

Source: Internet
Author: User
Tags rfc ssl certificate

I picked the address:

I'm having an error accessing an HTTPS Web site using requests

Error:14090086:ssl routines:SSL3_GET_SERVER_CERTIFICATE:certificate Verify failed

Find this article

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- ------------------------------------

Session Objects Session Object

The Session object allows you to adhere to certain parameters when requested. In addition, all requested cookies are persisted by the session instance.

Let's stick with the request

s = requests. Session ()

S.get (' ')

r = S.get ("Http://")

Print R.text

# ' {' "cookies": {"Sessioncookie": "123456789"}} '

The session can also be used to provide the default data for the requested method. This is done by providing the properties of the data Session object:

s = requests. Session ()

S.auth = (' user ', ' pass ')

S.headers.update ({' x-test ': ' True '})

# both ' x-test ' and ' x-test2 ' are sent

S.get (' ', headers={' x-test2 ': ' True '})

Any dictionary will be passed to the request method by the value of the setting of the merge session level. Method-level parameters override session parameters.

To take a value from a dictionary parameter

If you want to delete a parameter in a session, then you only need to set it to none, and he will automatically be deleted.

All values in a session are included directly with you. See the session API documentation for more information.

Request and Response objects

>>> r = requests.get (' Http:// ')


>>> r.headers

{' content-length ': ' 56170 ', ' x-content-type-options ': ' Nosniff ', ' X-cache ':

' Hits from Cp1006.eqiad.wmnet, MISS from Cp1010.eqiad.wmnet ', ' content-encoding ':

' gzip ', ' age ': ' 3080 ', ' content-language ': ' en ', ' vary ': ' Accept-encoding,cookie ',

' Server ': ' Apache ', ' last-modified ': ' Wed, June 01:33:50 GMT ',

' Connection ': ' Close ', ' cache-control ': ' Private, s-maxage=0, Max-age=0,

Must-revalidate ', ' Date ': ' Thu, June, 12:59:39 GMT ', ' content-type ':

' Text/html; Charset=utf-8 ', ' x-cache-lookup ': ' Hit fromcp1006.eqiad.wmnet:3128,

MISS from Cp1010.eqiad.wmnet:80 '}

However, if we want to get the header sent by our server, we just need to access the request and then request the header:

>>> r.request.headers

{' accept-encoding ': ' Identity, deflate, compress, gzip ',

' Accept ': ' */* ', ' user-agent ': ' python-requests/1.2.0 '}

Prepare the request

When you receive an response object in an API call or session call, the Request property is actually preparedrequest used. In some cases, before sending a request, you may wish to do some extra work on the body or head (or any other really). This simple recipe is as follows:

From requests import Request, Session

s = Session ()

prepped = Request (' GET ', # or any other method, ' POST ', ' PUT ', etc.




# ...

). Prepare ()

# do something with Prepped.body

# do something with Prepped.headers

RESP = S.send (prepped,






# etc.


Print (Resp.status_code)

Since you have not done anything special to request the object, you are ready to modify the Preparedrequest object immediately. You can then send additional parameters to the request that you want to send. * or in sesssion. *。

SSL Certificate Validation

Requests that you can validate an HTTPS request for an SSL certificate, just like a Web browser. To check the host's SSL certificate, you can use the checksum parameter:

>>> requests.get (' ', verify=true)

Requests.exceptions.SSLError:hostname ' ' doesn ' t matcheither of ' * ', ' '

I don't have SSL settings for this domain, so it's failed. Great on GitHub although not:

>>> requests.get (' ', verify=true)

<response [200]>

You can also verify the path of a private certificate Ca_bundle file by verifying it. You can also set the requests_ca_bundle of environment variables.

If you set the authentication setting to False, you can also ignore the authentication SSL certificate.

>>> requests.get (' ', cert= ('/path/server.crt ', '/path/key '))

<response [200]>

If you specify an incorrect path or an invalid certificate:

>>> requests.get (' ', cert= '/wrong_path/server.pem ')

Sslerror: [Errno 336265225] _ssl.c:347:error:140b0009:sslroutines:ssl_ctx_use_privatekey_file:pem Lib

Main content Workflow

By default, when you make a request, the body's response is to download it immediately. You can override this behavior and defer downloading the response to the body until you access the Response.content, with the properties of the stream parameter:

Tarball_url = ' Https:// '

R = Requests.get (Tarball_url, Stream=true)

Only at this point has the downloaded response headers and connections remain open, allowing us to enable content retrieval criteria:

if int (r.headers[' content-length ')) < too_long:

Content = R.content


You can further control the workflow of using the Response.iter_content and Response.iter_lines methods, or from the basic urllib3 urllib3. HttpResponse reading in Response.raw.

Stay active

It is important to note that the published connection returns to the pool and reuses all the data to be read:

Make sure that you set the data flow to false or read the content properties of the response object.

Stream upload

Request Support It allows you to send a large number of streams or file stream uploads that are not read into memory. To stream and upload, simply provide your body with a file-like object:

With open (' Massive-body ') as F: (' http://some.url/streamed ', data=f)

Block Encoding Request:

Also, support chunked transfer encoding outgoing and incoming requests. To send a block-encoded request, just provide a generator (or any iterator with no length) for your body:

Def gen ():

Yield ' Hi '

Yield ' there ' (' http://some.url/chunked ', Data=gen ())

Event Hooks:

The request has a hook system that you can use to handle the processing of part or signal events during the application process.

You can specify a hook function on the basis of each request, via the hook request parameter of a {hook_name:callback_function} dictionary:

Hooks=dict (Response=print_url)

That Callback_function will receive the data block as the first parameter.

>>> requests.get (' ', Hooks=dict (Response=print_url))

<response [200]>

Custom Authentication

This callback_function will receive a chunk of the data as the first parameter.

From Requests.auth import authbase

Class Pizzaauth (Authbase):

"" "Attaches HTTP Pizza authentication tothe given Request object." "

def __init__ (self, username):

# Setup any auth-related datahere

Self.username = Username

def __call__ (self, R):

# Modify and return the request

r.headers[' X-pizza '] =self.username

Return r

Then, we can make a request using our Pizza Auth:

>>> requests.get (' Http:// ', Auth=pizzaauth (' Kenneth '))

<response [200]>


Import requests

Proxies = {

"http": "",

"https": "",


Requests.get ("", proxies=proxies)

You can also configure the proxy server environment Http_proxy and Https_proxy.

$ export http_proxy= ""

$ export https_proxy= ""

$ python

>>> Import Requests

>>> requests.get ("")

Use the Http://user:[email protected]/syntax for HTTP Basic Auth with your proxy:

Proxies = {

"http": "Http://user:[email protected]:3128/",


Comply with:

Requirements are in order to comply with the relevant specifications and RFC compliance and will not cause difficulties for the user. This attention may lead to some seemingly unusual behavior that may be unfamiliar to those norms.


If there is no explicit character set, the Content-type header in the HTTP header contains the text. In this case, RFC 2616 specifies that the default character set must be iso-8859-1

HTTP verb

The request provides access to almost omni-directional http verbs: get,options,head,post,put,patch and delete. The following provides a detailed example of using these different verbs in the request, using GitHub's API.

>>> Import Requests

>>> R =requests.get (' A050FAF084662F3A352DD1A941F2C7C9F886D4AD ')

So, GitHub returns JSON. We can use the R.json method to parse to a Python object.

>>> commit_data = R.json ()

>>> Print Commit_data.keys ()

[u ' committer ', U ' author ', U ' url ', U ' tree ', U ' sha ', U ' parents ', u ' message ']

>>> print commit_data[u ' committer ']

{u ' date ': U ' 2012-05-10t11:10:50-07:00 ', u ' email ': u ' [email protected] ', U ' name ': U ' Kenneth Reitz '}

>>> print commit_data[u ' message '

Makin ' History

Requests can be easily used in various forms of authentication, including very common basic authentication.

>>> from Requests.auth import Httpbasicauth

>>> auth = Httpbasicauth (' [email protected] ', ' Not_a_real_password ')

>>> r = (Url=url, Data=body, Auth=auth)

>>> R.status_code


>>> content = R.json ()

>>> print content[u ' body ']

Sounds great! I ' ll get right on it.

(GO) Python requests advanced usage--including solutions for SSL certificate errors

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: 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.