HCE Project Python language Distributed Tasks Manager Application, Distributed Crawler Application and client API bindings.  2.0.0-chaika
Hierarchical Cluster Engine Python language binding
ftest_is_host_available.py
Go to the documentation of this file.
1 #coding: utf-8
2 '''
3 HCE project, Python bindings, DC dependencies
4 The isHostAvailable method tests.
5 
6 @package: drce
7 @author bgv bgv.hce@gmail.com
8 @link: http://hierarchical-cluster-engine.com/
9 @copyright: Copyright © 2016 IOIX Ukraine
10 @license: http://hierarchical-cluster-engine.com/license/
11 @since: 0.1
12 '''
13 
14 
15 def isHostAvailable(url, parameters, logger=None, timeout=0.5):
16  ret = True
17 
18  try:
19  if 'method' in parameters and int(parameters['method']) == 0:
20  from urlparse import urlparse
21  pr = urlparse(url)
22  print str(pr)
23  pr = pr.netloc.split(':')
24  if len(pr) == 1:
25  port = 80
26  else:
27  port = pr[1]
28  host = pr[0]
29  print host, port
30  if 'domain_name_resolve' in parameters and int(parameters['domain_name_resolve']) == 1:
31  import socket
32  ai = socket.getaddrinfo(host, port, 0, 0, socket.IPPROTO_TCP)
33  print ai
34  if 'connect_resolve' in parameters and int(parameters['connect_resolve']) == 1:
35  if 'connection_timeout' in parameters and float(parameters['connection_timeout']) > 0:
36  timeout = float(parameters['connection_timeout'])
37  for item in ai:
38  af, socktype, proto, canonname, sa = item
39  s = socket.socket(af, socktype, proto)
40  s.settimeout(float(timeout))
41  try:
42  s.connect(sa)
43  except Exception, err:
44  ret = False
45  print 'ERROR:', str(sa), str(err)
46  if logger is not None:
47  logger.debug("Host %s connect check error: %s", str(sa), str(err))
48  continue
49  s.close()
50  ret = True
51  break
52 
53  except Exception, err:
54  ret = False
55  print 'ERROR:', str(err)
56  if logger is not None:
57  logger.debug("Host %s availability check error: %s", str(url), str(err))
58 
59  return ret
60 
61 
62 p = {"method":0, "domain_name_resolve":1, "connect_resolve":1, "connection_timeout":0.5}
63 #print isHostAvailable('http://rss.feedsportal.com/a', p)
64 #print isHostAvailable('http://docs.python.org', p)
65 #print isHostAvailable('http://192.168.0.1', p)
66 #print isHostAvailable('http://54.191.242.90:8083', p)
67 print isHostAvailable('http://108.59.13.38:13010', p)
68 
def isHostAvailable(url, parameters, logger=None, timeout=0.5)