Skip to content

v0.8.3 bug fix: ping endpoint #59

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 16 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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