Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
adbar committed May 31, 2024
1 parent 138f63c commit 689a517
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions courlan/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

import logging
import ssl
import urllib.request

from typing import Optional
from urllib.error import HTTPError
from urllib import request

import certifi

Expand All @@ -31,14 +29,12 @@ def redirection_test(url: str) -> str:
Nothing.
"""
try:
req = urllib.request.Request(url, method="HEAD")
rhead = urllib.request.urlopen(req, context=CERTIFI_CONTEXT)
except HTTPError as error:
if error.status in ACCEPTABLE_CODES:
return error.url
req = request.Request(url, method="HEAD")
with request.urlopen(req, context=CERTIFI_CONTEXT) as f:
pass
if f.status in ACCEPTABLE_CODES:
return f.url
except Exception as err:
LOGGER.warning("unknown error: %s %s", url, err)
else:
return rhead.url

raise ValueError(f"cannot reach URL: {url}")

0 comments on commit 689a517

Please sign in to comment.