Skip to content

How to force Client to use a specific TLS version? #469

Closed

Description

@qwerty32123

So basically im migrating from requests to httpx and i see i was forcing tlsv1_2 with requests HTTPadapter , but i dont think these is comp with httpx, i was wondering how i can force httpx to use specific tls version

from requests.adapters import HTTPAdapter
from urllib3.poolmanager import PoolManager
import ssl


class MyAdapter(HTTPAdapter):
    def init_poolmanager(self, connections, maxsize, block=False):
        self.poolmanager = PoolManager(num_pools=connections,
                                       maxsize=maxsize,
                                       block=block,
                                       ssl_version=ssl.PROTOCOL_TLSv1_2)



....



session = requests.session()
session.mount('https://', MyAdapter())

I was doing like these in requests

Activity

  1. florimondmanca commented on Oct 10, 2019

    @florimondmanca
    Contributor

    Thanks for opening. Since I believe TLS v1.2 is considered insecure, I suppose you were forcing 1.2 because the client you’re connecting to is exposing that version of TLS? If you don’t have such a requirement anymore I’d advocate in favor of using 1.3.

    If you really need it though, there may be an undocumented API (most likely SSLConfig) that you could somehow override, but I can’t dig it out right now.

  2. qwerty32123 commented on Oct 10, 2019

    @qwerty32123
    Author

    I need to force 1.2 for now, i have no choice cause the app im developing requires it, im sniffing http calls and copying them basically and they are made with tls 1.2
    How i could force it then? i would have to edit httpx files? Thanks for answer @florimondmanca

  3. sethmlarson commented on Oct 10, 2019

    @sethmlarson
    Contributor

    TLSv1.2 is totally secure and recommended, we enforce TLSv1.2+ for all of our connections by default.

    @GnubiBORED Are you trying to enforce exactly TLSv1.2 (meaning explicitly excluding TLSv1.3?)

  4. changed the title [-]Force Client to use Specific Tls version?[/-] [+]How to force Client to use a specific TLS version?[/+] on Oct 10, 2019
  5. qwerty32123 commented on Oct 10, 2019

    @qwerty32123
    Author

    TLSv1.2 is totally secure and recommended, we enforce TLSv1.2+ for all of our connections by default.

    @GnubiBORED Are you trying to enforce exactly TLSv1.2 (meaning explicitly excluding TLSv1.3?)

    Yes, i need to in all my http calls just use TLSv1.2 never 1.3 atleast from now, cause the android app im sniffing uses 1.2 and i want to make the calls in the same way, with requests and httpadapter was easy to do. What im asking is if its possible to force specific tls version while making calls

  6. sethmlarson commented on Oct 10, 2019

    @sethmlarson
    Contributor

    So the way I'd recommend accomplishing this:

    >>> import ssl
    >>> import httpx
    >>> ssl_config = httpx.SSLConfig()
    >>> ssl_context = ssl_config.load_ssl_context()
    >>> ssl_context.options |= getattr(ssl, "OP_NO_TLSv1_3", 0)
    >>> client = httpx.Client(verify=ssl_context)
    >>> resp = client.get("https://howsmyssl.com/a/check")
    >>> print(resp.json()["tls_version"])
    'TLSv1.2'

    Currently we don't allow passing an SSLConfig object to verify= (which maybe we should because this is painful)

  7. qwerty32123 commented on Oct 10, 2019

    @qwerty32123
    Author

    @sethmlarson but i need to verify request with charlesproxy ssl certificate too, these will be a problem?

  8. sethmlarson commented on Oct 10, 2019

    @sethmlarson
    Contributor

    You can pass verify=[path to cert] into the constructor of the httpx.SSLConfig() and that should work.

  9. sethmlarson commented on Oct 10, 2019

    @sethmlarson
    Contributor

    Either that or you can call ssl_context.load_verify_locations([path to cert]) on the SSLContext object. That'll also work.

  10. qwerty32123 commented on Oct 10, 2019

    @qwerty32123
    Author

    Thanks for everything as always, i will close the issue

  11. alie42 commented on Dec 10, 2025

    @alie42

    لذا، بشكل أساسي، أقوم بالانتقال من الطلبات إلى httpx وأرى أنني كنت أجبر tlsv1_2 باستخدام طلبات HTTPadapter، لكنني لا أعتقد أن هذا متوافق مع httpx، كنت أتساءل كيف يمكنني إجبار httpx على استخدام إصدار tls محدد

    from requests.adapters import HTTPAdapter
    from urllib3.poolmanager import PoolManager
    import ssl
    
    
    class MyAdapter(HTTPAdapter):
        def init_poolmanager(self, connections, maxsize, block=False):
            self.poolmanager = PoolManager(num_pools=connections,
                                           maxsize=maxsize,
                                           block=block,
                                           ssl_version=ssl.PROTOCOL_TLSv1_2)
    
    
    
    ....
    
    
    
    session = requests.session()
    session.mount('https://', MyAdapter())
    

    كنت أفعل مثل هذه في الطلبات

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    tls+pkiIssues and PRs related to TLS and PKI

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions