isup: show state as 'down' if response is an HTTPError

This commit is contained in:
jkhsjdhjs 2021-07-20 21:39:39 +00:00
parent b6b8d91592
commit fe79f444cf

View File

@ -12,19 +12,22 @@ from . import Plugin
class Useless(Plugin): class Useless(Plugin):
@command @command
def isup(self, mask: IrcString, target: IrcString, args: Dict): def isup(self, mask: IrcString, target: IrcString, args: Dict):
"""Checks if a address is up or down """Checks if an address is up or down
%%isup <address> %%isup <address>
""" """
address = args['<address>'] address = args['<address>']
if not address.startswith('http://'): if not address.startswith('http://') and not address.startswith('https://'):
address = 'https://{}'.format(address) address = 'https://{}'.format(address)
try: try:
requests.head(address) res = requests.head(address)
res.raise_for_status()
state = 'up' state = 'up'
except requests.ConnectionError: except requests.ConnectionError:
state = 'down' state = 'down (timeout)'
except requests.HTTPError as e:
state = f'down ({e.response.status_code} {e.response.reason})'
parsed = urlparse(address) parsed = urlparse(address)
return '\x02{}://{}\x02 seems to be \x02{}\x02'.format(parsed.scheme, parsed.netloc, state) return '\x02{}://{}\x02 seems to be \x02{}\x02'.format(parsed.scheme, parsed.netloc, state)