Skip to content

Commit

Permalink
Merge pull request #61 from yangruihan/master
Browse files Browse the repository at this point in the history
add log error for ping exception
  • Loading branch information
wj-Mcat authored Aug 6, 2021
2 parents d1c0c38 + 8d01dcc commit 3b76f5c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.4
0.8.5
34 changes: 27 additions & 7 deletions src/wechaty_puppet_service/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@
import socket
from logging import Logger
from typing import Optional, Tuple
from ping3 import ping # type: ignore
from ping3 import ping, errors # type: ignore
from wechaty_puppet.exceptions import WechatyPuppetConfigurationError # type: ignore


def extract_host_and_port(url: str) -> Tuple[str, Optional[int]]:
"""it shoule be <host>:<port> format, but it can be a service name.
If it's in docker cluster network, the port can be None
"""
It shoule be <host>:<port> format, but it can be a service name.
If it's in docker cluster network, the port can be None.
Args:
url (str): the service endpoint or names
Return:
host (str), port (Optional[int])
"""
if ':' in url:
host, port = url.split(':')
Expand All @@ -49,16 +53,32 @@ def extract_host_and_port(url: str) -> Tuple[str, Optional[int]]:

def test_endpoint(end_point: str, log: Logger) -> int:
"""
test_endpoint:
1.If there is port: telnet
2.If there is host/domain: ping or
Check end point is valid
Use different method:
1. If there is port: telnet
2. If there is host/domain: ping
Args:
end_point (str): host and port
log (Logger): for debug
Return:
return True if end point is valid, otherwise False
"""
tn = Telnet()
host, port = extract_host_and_port(end_point)

res = True
if port is None:
if ping(host) is False:
try:
if ping(host) is False:
res = False
except PermissionError as pe:
log.error(pe)
res = False
except errors.PingError as e:
log.error(f'got a ping error:\n{e}')
res = False
else:
try:
Expand Down

0 comments on commit 3b76f5c

Please sign in to comment.