Python 3.7, Win10. There is a function for ping (taken from the answer ), which refers to the system ping. But you need not only an assessment of the availability of the host, but you also need to get the response time (parsip). I can not understand:

  1. why output to int
  2. how to convert to normal encoding
  3. how to suppress this output and convert to str for further parsing

Result of work:

          ⠬    google.com [216.58.215.78]   32    ⠬        :  ⢥     216.58.215.78:  ᫮     =32  ६ =27   TTL=55     ⨪  Ping     216.58.215.78:     ⮢:   ࠢ     = 1,     祭  = 1,     ﭮ = 0 (0%      )  ਡ    ⥫쭮   ६   ਥ  -  ।       :        쭮  = 27 ᥪ,    ᨬ  쭮  = 27  ᥪ,  ।    = 27  ᥪ type con_out <class 'int'> s 0 encoded b'0' type: <class 'int'> getdefaultencoding: utf-8 locale: cp1251 stdout.encoding: UTF-8 stdin.encoding: UTF-8 Process finished with exit code 0 

Code:

  import sys import locale import subprocess import platform def ping(host): ping_str = "-n 1" if platform.system().lower() == "windows" else "-c 1" args = "ping " + " " + ping_str + " " + host con_out = subprocess.call(args) return con_out ping_out = ping("google.com") string = str(ping_out) print('s', string) print('encoded ', string.encode('utf8')) print('type: ', type(ping_out)) print('getdefaultencoding: ', sys.getdefaultencoding()) print('locale: ', locale.getpreferredencoding()) print('stdout.encoding: ', sys.stdout.encoding) print('stdin.encoding: ', sys.stdin.encoding) 

1 answer 1

 import sys import locale import subprocess import platform def ping(host): ping_str = "-n 1" if platform.system().lower() == "windows" else "-c 1" args = "ping " + " " + ping_str + " " + host con_out = subprocess.check_output(args, shell=True).decode('cp866') return con_out ping_out = ping("google.com") string = str(ping_out) print('s', string) print('encoded ', string.encode('utf8')) print('type: ', type(ping_out)) print('getdefaultencoding: ', sys.getdefaultencoding()) print('locale: ', locale.getpreferredencoding()) print('stdout.encoding: ', sys.stdout.encoding) print('stdin.encoding: ', sys.stdin.encoding)