File: mysocket.py

package info (click to toggle)
nufw 2.4.3-2.2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,728 kB
  • sloc: ansic: 32,174; sh: 10,146; python: 4,176; makefile: 965; perl: 630; lex: 176; php: 168; yacc: 117; xml: 34
file content (42 lines) | stat: -rw-r--r-- 1,426 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from socket import (socket,
    AF_INET, SOCK_STREAM,
    error as socket_error, timeout as socket_timeout)
from logging import info
from errno import ETIMEDOUT, ENETUNREACH, EISCONN

def connectTcp(host, port, timeout):
    """
    timeout can be 'None' (no timeout)
    """
    try:
        conn = socket(AF_INET, SOCK_STREAM)
        conn.settimeout(timeout)
        conn.connect((host,port))
        conn.close()
        info("connectTcp(%s, %s, timeout=%s): success" % (host, port, timeout))
        return True
    except socket_timeout:
        info("connectTcp(%s, %s, timeout=%s): timeout" % (host, port, timeout))
        return False
    except socket_error, err:
        info("connectTcp(%s, %s, timeout=%s): socket error: %s" % (host, port, timeout, err))
        return False

def connectTcpFail(host, port, timeout):
    """
    timeout can be 'None' (no timeout)
    """
    try:
        conn = socket(AF_INET, SOCK_STREAM)
        conn.settimeout(timeout)
        conn.connect((host,port))
        conn.close()
        info("connectTcp(%s, %s, timeout=%s): success" % (host, port, timeout))
        return EISCONN
    except socket_timeout:
        info("connectTcp(%s, %s, timeout=%s): timeout" % (host, port, timeout))
        return ETIMEDOUT
    except socket_error, (code, msg):
        info("connectTcp(%s, %s, timeout=%s): socket error: %s, %s" % (host, port, timeout, code, msg))
        return code