Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Unknown keyword arguments now raise `TypeError` when calling async or blocking client constructors, HTTP or WebSocket request methods and functions, `Http1Options`, `Http2Options`, `TlsOptions`, or `Proxy` factories. These arguments were previously ignored.
- Set client-only options such as `connect_timeout` and `proxies` on `Client`; passing them to a request now raises `TypeError`. Use `proxy` to select a proxy for one request.
- Async request calls check keyword names before returning a coroutine. Values for supported request keywords are still converted on first `await` or entry with `async with`.
- System DNS is consistently used by default. Previously, the behavior differed between no kwargs and any unrelated kwarg being set.

## [0.11.0](https://github.com/0x676e67/wreq-python/compare/v0.10.2..v0.11.0) - 2026-04-13

Expand Down
6 changes: 3 additions & 3 deletions docs/source/guide/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ enable logging only when you need it.
## DNS and protocol options

`dns_options` selects the resolver and can override specific host addresses.
Overrides retain the URL's hostname for HTTP and TLS. This configuration uses
system DNS; omit it to use the default Hickory resolver:
Overrides retain the URL's hostname for HTTP and TLS. Set `system_dns=False` to
use the Hickory resolver instead of system DNS (such as `getaddrinfo` on Linux).

```python
from wreq.blocking import Client
from wreq.dns import DnsOptions


with Client(dns_options=DnsOptions(system_dns=True)) as client:
with Client(dns_options=DnsOptions(system_dns=False)) as client:
with client.get("https://httpbin.io/get") as response:
print(response.text())
```
Expand Down
2 changes: 1 addition & 1 deletion python/wreq/dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class DnsOptions:

def __init__(
self,
system_dns: bool = False,
system_dns: bool = True,
lookup_ip_strategy: LookupIpStrategy = LookupIpStrategy.IPV4_AND_IPV6,
) -> None:
"""Create a new DnsOptions.
Expand Down
7 changes: 2 additions & 5 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use self::{
use crate::{
cookie::Jar,
coroutine::{self, Coroutine},
dns::{DnsOptions, HickoryResolver, LookupIpStrategy},
dns::{DnsOptions, HickoryResolver},
emulate::EmulationLike,
error::Error,
extractor::Extractor,
Expand Down Expand Up @@ -488,10 +488,7 @@ impl Client {
builder =
builder.dns_resolver(HickoryResolver::new(opts.lookup_ip_strategy)?);
}
} else {
builder =
builder.dns_resolver(HickoryResolver::new(LookupIpStrategy::default())?);
};
}

// Compression options.
apply_option!(set_if_some, builder, config.gzip, gzip);
Expand Down
2 changes: 1 addition & 1 deletion src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl DnsOptions {
/// `lookup_ip_strategy` only applies to hickory and has no effect when
/// using the system DNS resolver.
#[new]
#[pyo3(signature=(system_dns = false, lookup_ip_strategy = LookupIpStrategy::IPV4_AND_IPV6))]
#[pyo3(signature=(system_dns = true, lookup_ip_strategy = LookupIpStrategy::IPV4_AND_IPV6))]
pub fn new(system_dns: bool, lookup_ip_strategy: LookupIpStrategy) -> Self {
DnsOptions {
system_dns,
Expand Down
70 changes: 60 additions & 10 deletions tests/dns_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import asyncio
import platform
import threading
from datetime import timedelta
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from ipaddress import IPv4Address

import pytest

import wreq
from wreq import blocking
from wreq.dns import DnsOptions, LookupIpStrategy
Expand Down Expand Up @@ -46,21 +47,26 @@ async def fetch(host, options):
if blocking_api:

def request():
with blocking.Client(**kwargs) as client:
with client.get(url) as response:
return response.text()
with blocking.Client(**kwargs) as client, client.get(url) as response:
return response.text()

body = await asyncio.to_thread(request)
else:
async with wreq.Client(**kwargs) as client:
async with client.get(url) as response:
body = await response.text()
async with wreq.Client(**kwargs) as client, client.get(url) as response:
body = await response.text()
assert body == "DNS resolved"

# Exercise actual hostname resolution, including the default resolver.
await fetch("localhost", None)
await fetch("localhost", DnsOptions(system_dns=True))
await fetch("localhost", DnsOptions(lookup_ip_strategy=LookupIpStrategy.IPV4_ONLY))
await fetch(
"localhost",
DnsOptions(system_dns=False, lookup_ip_strategy=LookupIpStrategy.IPV4_ONLY),
)

options = DnsOptions(system_dns=True)
options.add_resolve("dns-test.invalid", [IPv4Address("127.0.0.1")])
await fetch("dns-test.invalid", options)

# Every strategy must initialize and preserve explicit address overrides.
for strategy in (
Expand All @@ -70,17 +76,61 @@ def request():
LookupIpStrategy.IPV6_THEN_IPV4,
LookupIpStrategy.IPV4_THEN_IPV6,
):
options = DnsOptions(lookup_ip_strategy=strategy)
options = DnsOptions(system_dns=False, lookup_ip_strategy=strategy)
options.add_resolve("dns-test.invalid", [IPv4Address("127.0.0.1")])
await fetch("dns-test.invalid", options)


@pytest.mark.asyncio
async def test_dns_resolve_override(dns_http_server):
# An override wins over the real address of a resolvable host.
dns_options = DnsOptions(lookup_ip_strategy=LookupIpStrategy.IPV4_ONLY)
dns_options = DnsOptions(
system_dns=False, lookup_ip_strategy=LookupIpStrategy.IPV4_ONLY
)
dns_options.add_resolve("www.google.com", [IPv4Address("127.0.0.1")])
async with wreq.Client(dns_options=dns_options, no_proxy=True) as client:
url = f"http://www.google.com:{dns_http_server}/"
async with await asyncio.wait_for(client.get(url), 5) as response:
assert await response.text() == "DNS resolved"


async def invalid_name_resolve_error(client: wreq.Client | blocking.Client) -> str:
# A 64-byte label is not a valid DNS name, so every resolver fails without
# sending a query, each with its own error.
url = f"http://{'a' * 64}.invalid/"

def request_blocking(blocking_client: blocking.Client) -> None:
with blocking_client, blocking_client.get(url, timeout=timedelta(seconds=5)):
pass

with pytest.raises(wreq.exceptions.ConnectionError) as exc_info:
if isinstance(client, blocking.Client):
await asyncio.to_thread(request_blocking, client)
else:
async with client, await asyncio.wait_for(client.get(url), 5):
pass
return str(exc_info.value)


@pytest.mark.asyncio
@pytest.mark.parametrize(
"use_dns_options", [False, True], ids=["no_kwargs", "dns_options"]
)
@pytest.mark.parametrize(
"client_type", [wreq.Client, blocking.Client], ids=["async", "blocking"]
)
async def test_default_dns_resolver_is_getaddrinfo(
use_dns_options: bool, client_type: type[wreq.Client | blocking.Client]
):
# Control: hickory rejects the name while parsing it. If this stops holding,
# the assertions below no longer tell the resolvers apart.
hickory_client = client_type(dns_options=DnsOptions(system_dns=False))
assert "LabelBytesTooLong" in await invalid_name_resolve_error(hickory_client)

client = client_type(dns_options=DnsOptions()) if use_dns_options else client_type()
error = await invalid_name_resolve_error(client)
assert "LabelBytesTooLong" not in error
if platform.system() != "Windows":
# std's getaddrinfo wrapper prefixes every EAI_* failure with this;
# Windows reports a localized WSA error instead.
assert "failed to lookup address information" in error
Loading