encode / httpx

A next generation HTTP client for Python. 🦋

Home Page:https://www.python-httpx.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't open the URL where the domain is set to resolve in /etc/hosts.

fsrm365 opened this issue · comments

image

The domain crawl_api is set in /etc/hosts as
127.0.0.1 crawl_api

I can open it with "requests". But not with "httpx".

httpx version: 0.25.0
python version: 3.11.5

Currently there is no support for hosts file, but here is simple solution using custom host transport:

import httpx

class NameSolver(dict):
    def resolve(self, request: httpx.Request) -> httpx.Request:
        host = request.url.host
        ip = self.get(host)

        if ip:
            request.extensions["sni_hostname"] = host
            request.url = request.url.copy_with(host=ip)

        return request

class CustomHost(httpx.HTTPTransport):
    def __init__(self, solver: NameSolver, *args, **kwargs) -> None:
        self.solver = solver
        super().__init__(*args, **kwargs)

    def handle_request(self, request: httpx.Request) -> httpx.Response:
        request = self.solver.resolve(request)
        return super().handle_request(request)

Use it as:

ns = NameSolver({"crawl_api": "127.0.0.1"})  # or load `hosts` file content here

with httpx.Client(transport=CustomHost(ns)) as client:
    r = client.get("http://crawl_api:8000/")