diff options
Diffstat (limited to '')
| -rw-r--r-- | webmentions_ssg/net.py | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/webmentions_ssg/net.py b/webmentions_ssg/net.py index 01a9a80..b88e2d8 100644 --- a/webmentions_ssg/net.py +++ b/webmentions_ssg/net.py @@ -110,21 +110,28 @@ class PublicNetworkBackend(httpcore.SyncBackend): else socket.AF_INET ) sock = socket.socket(family, socket.SOCK_STREAM) + try: sock.settimeout( self._deadline.timeout(timeout, httpcore.ConnectTimeout) ) + if local_address is not None: sock.bind((local_address, 0)) + for option in socket_options or (): sock.setsockopt(*option) + sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) + # This numeric address is the *same* address just validated; # no getaddrinfo or DNS resolver runs between check and connect. sock.connect((address, port)) + return DeadlineStream(SyncStream(sock), self._deadline) except (TimeoutError, OSError) as exc: sock.close() + if isinstance(exc, TimeoutError): last_error = httpcore.ConnectTimeout(str(exc)) else: @@ -157,6 +164,7 @@ def build_client(user_agent: str, *, accept: str | None = None) -> httpx.Client: headers = {"User-Agent": user_agent} if accept is not None: headers["Accept"] = accept + return httpx.Client( headers=headers, timeout=httpx.Timeout(current_app.config["REQUEST_TIMEOUT"]), @@ -225,8 +233,7 @@ def public_addresses( def is_http_url(url: str) -> bool: - """ - Check whether a URL is an absolute HTTP or HTTPS URL. + """Check whether a URL is an absolute HTTP or HTTPS URL. :param url: URL to validate. :return: Whether the URL uses HTTP or HTTPS and contains a hostname. |
