Skip to content

Commit 9f2f3bd

Browse files
committed
Set an explicit User-Agent and remove port out of Host.
1 parent e3e18fb commit 9f2f3bd

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/holdup/cli.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@
5353
from urllib.request import HTTPDigestAuthHandler
5454
from urllib.request import HTTPPasswordMgrWithDefaultRealm
5555
from urllib.request import HTTPSHandler
56+
from urllib.request import Request
5657
from urllib.request import build_opener
5758

59+
import holdup
60+
5861

5962
class Check:
6063
error = None
@@ -158,6 +161,7 @@ def __init__(self, url):
158161
self.netloc = f"{url.hostname}:{url.port}"
159162
else:
160163
self.netloc = url.hostname
164+
self.host = url.hostname
161165

162166
cleaned_url = urlunparse(url._replace(netloc=self.netloc))
163167

@@ -180,8 +184,9 @@ def run(self, options):
180184
handlers.append(HTTPSHandler(context=ssl_ctx))
181185

182186
opener = build_opener(*handlers)
183-
184-
with closing(opener.open(self.url, timeout=options.check_timeout)) as req:
187+
opener.addheaders = [("User-Agent", f"python-holdup/{holdup.__version__}")]
188+
request = Request(self.url, headers={"Host": self.host}) # noqa: S310
189+
with closing(opener.open(request, timeout=options.check_timeout)) as req:
185190
status = req.getcode()
186191
if status != 200:
187192
raise Exception(f"Expected status code 200, got {status!r}")
@@ -222,6 +227,8 @@ def __init__(self, path):
222227
self.path = path
223228

224229
def run(self, _):
230+
# necessary to check if it exists.
231+
os.stat(self.path) # noqa: PTH116
225232
if not os.access(self.path, os.R_OK):
226233
raise Exception(f"Failed access({self.path!r}, R_OK) test")
227234

@@ -379,8 +386,6 @@ def parse_value(value, proto):
379386

380387

381388
def add_version_argument(parser):
382-
import holdup
383-
384389
parser.add_argument(
385390
"--version",
386391
action="version",

0 commit comments

Comments
 (0)