Skip to content

Commit

Permalink
Merge pull request #59 from AIAmber/master
Browse files Browse the repository at this point in the history
v0.8.3 bug fix: ping endpoint
  • Loading branch information
wj-Mcat authored Aug 4, 2021
2 parents 9b109f2 + 1929089 commit d1c0c38
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.3
0.8.4
8 changes: 5 additions & 3 deletions src/wechaty_puppet_service/puppet.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from grpclib.client import Channel
# pylint: disable=E0401
from pyee import AsyncIOEventEmitter # type: ignore
from ping3 import ping # type: ignore
from wechaty_puppet.schemas.types import PayloadType # type: ignore

from wechaty_puppet import ( # type: ignore
Expand Down Expand Up @@ -82,7 +81,10 @@
get_endpoint,
get_token,
)
from wechaty_puppet_service.utils import extract_host_and_port
from wechaty_puppet_service.utils import (
extract_host_and_port,
test_endpoint
)

log = get_logger('HostiePuppet')

Expand Down Expand Up @@ -898,7 +900,7 @@ def _init_puppet(self):
log.debug('get puppet ip address : <%s>', data)
self.options.end_point = '{ip}:{port}'.format(**data)

if ping(self.options.end_point) is False:
if test_endpoint(self.options.end_point, log) is False:
raise WechatyPuppetConfigurationError(
f"can't not ping endpoint: {self.options.end_point}"
)
Expand Down
29 changes: 28 additions & 1 deletion src/wechaty_puppet_service/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
"""
from __future__ import annotations

from telnetlib import Telnet

import socket
from logging import Logger
from typing import Optional, Tuple
from wechaty_puppet.exceptions import WechatyPuppetConfigurationError # type: ignore
from ping3 import ping # type: ignore
from wechaty_puppet.exceptions import WechatyPuppetConfigurationError # type: ignore


def extract_host_and_port(url: str) -> Tuple[str, Optional[int]]:
Expand All @@ -40,3 +45,25 @@ def extract_host_and_port(url: str) -> Tuple[str, Optional[int]]:
return host, int(port)

return url, None


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
"""
tn = Telnet()
host, port = extract_host_and_port(end_point)

res = True
if port is None:
if ping(host) is False:
res = False
else:
try:
tn.open(host, port=port)
except socket.error as e:
log.error(e)
res = False
return res

0 comments on commit d1c0c38

Please sign in to comment.