# Path: zabby/core/exceptions.py
# class WrongArgumentError(Exception):
#     """ Wrong argument was passed to a function"""
# 
# Path: zabby/core/utils.py
# def validate_mode(mode, available_modes):
#     """
#     Checks if mode is one of available_modes
# 
#     :raises: WrongArgumentError if mode is not one of available_modes
#     """
#     if mode not in available_modes:
#         raise WrongArgumentError(
#             "Unknown mode '{mode}' should be one of {modes}".format(
#                 mode=mode, modes=available_modes))
# 
# def tcp_communication(port, host='localhost', requests=list(),
#                       receive_first=False, timeout=1.0):
#     """
#     Connects to port, optionally sending requests and returns any responses
# 
#     :param requests: list of binary objects that will be sent in order
#         it is expected that there will be a response for every request
#     :param receive_first: if true will try to receive data before sending any
#         requests
# 
#     :raises: IOError, no exception handling is done in this function, most
#         exceptions will be socket exceptions
#     """
#     if any([not isinstance(request, binary_type) for request in requests]):
#         raise WrongArgumentError("Every request should be in binary. "
#                                  "Requests: '{0}'".format(requests))
# 
#     conn = None
#     responses = list()
#     try:
#         conn = socket.create_connection((host, port), timeout=timeout)
#         if receive_first:
#             responses.append(conn.recv(4096))
# 
#         for request in requests:
#             conn.sendall(request)
#             responses.append(conn.recv(4096))
# 
#     finally:
#         if conn is not None:
#             conn.close()
# 
#     return responses


import logging
import re
from zabby.core.exceptions import WrongArgumentError
from zabby.core.utils import validate_mode, tcp_communication
    Returns 1 if service running on port accepts connections and behaves as
    expected, 0 otherwise

    :param service_name: specifies expected behaviour and port
        ssh:
            behavior: should respond with a greeting message upon connection
            port: 22
    :param port: overrides port specified by service_name

    :raises: WrongArgumentError if unsupported service_name is supplied,
        port is not an integer in range [0,65535] or
        timeout is not a positive float
    """
    validate_mode(service_name, SERVICES.keys())
    if port:
        try:
            port = int(port)
            if port < 0 or 65535 < port:
                raise ValueError()
        except ValueError:
            raise WrongArgumentError(
                "Port must be an integer in range [0,65535], got '{0}'".format(
                    port))
    else:
        port = SERVICES[service_name]
    try:
        timeout = float(timeout)
        if timeout < 0.0:
            raise ValueError()
    except:

# Path: zabby/core/exceptions.py
# class WrongArgumentError(Exception):
#     """ Wrong argument was passed to a function"""
# 
# Path: zabby/core/utils.py
# def validate_mode(mode, available_modes):
#     """
#     Checks if mode is one of available_modes
# 
#     :raises: WrongArgumentError if mode is not one of available_modes
#     """
#     if mode not in available_modes:
#         raise WrongArgumentError(
#             "Unknown mode '{mode}' should be one of {modes}".format(
#                 mode=mode, modes=available_modes))
# 
# def tcp_communication(port, host='localhost', requests=list(),
#                       receive_first=False, timeout=1.0):
#     """
#     Connects to port, optionally sending requests and returns any responses
# 
#     :param requests: list of binary objects that will be sent in order
#         it is expected that there will be a response for every request
#     :param receive_first: if true will try to receive data before sending any
#         requests
# 
#     :raises: IOError, no exception handling is done in this function, most
#         exceptions will be socket exceptions
#     """
#     if any([not isinstance(request, binary_type) for request in requests]):
#         raise WrongArgumentError("Every request should be in binary. "
#                                  "Requests: '{0}'".format(requests))
# 
#     conn = None
#     responses = list()
#     try:
#         conn = socket.create_connection((host, port), timeout=timeout)
#         if receive_first:
#             responses.append(conn.recv(4096))
# 
#         for request in requests:
#             conn.sendall(request)
#             responses.append(conn.recv(4096))
# 
#     finally:
#         if conn is not None:
#             conn.close()
# 
#     return responses


# Path: zabby/items/net/tcp.py
import logging
import re
from zabby.core.exceptions import WrongArgumentError
from zabby.core.utils import validate_mode, tcp_communication

    Returns 1 if service running on port accepts connections and behaves as
    expected, 0 otherwise

    :param service_name: specifies expected behaviour and port
        ssh:
            behavior: should respond with a greeting message upon connection
            port: 22
    :param port: overrides port specified by service_name

    :raises: WrongArgumentError if unsupported service_name is supplied,
        port is not an integer in range [0,65535] or
        timeout is not a positive float
    """
    validate_mode(service_name, SERVICES.keys())
    if port:
        try:
            port = int(port)
            if port < 0 or 65535 < port:
                raise ValueError()
        except ValueError:
            raise WrongArgumentError(
                "Port must be an integer in range [0,65535], got '{0}'".format(
                    port))
    else:
        port = SERVICES[service_name]
    try:
        timeout = float(timeout)
        if timeout < 0.0:
            raise ValueError()
    except:

        raise WrongArgumentError(