cup.net package

Submodules

cup.net.route module

description:host route info related module
class cup.net.route.RouteInfo[source]

Bases: object

Handler of Route Info for Linux system, for ipv4 only.

E.g.

from cup.net import route
ri = route.RouteInfo()
print json.dumps(ri.get_route_by_ip('10.32.19.92'), indent=1)
print json.dumps(ri.get_routes(), indent=1)

Return

{
 "Use": "0",
 "Iface": "eth1",
 "Metric": "0",
 "Destination": "10.0.0.0",
 "Mask": "255.0.0.0",
 "RefCnt": "0",
 "MTU": "0",
 "Window": "0",
 "Gateway": "10.226.71.1",
 "Flags": "0003",
 "IRTT": "0"
}
[
 {
  "Use": "0",
  "Iface": "eth1",
  "Metric": "0",
  "Destination": "10.226.71.0",
  "Mask": "255.255.255.0",
  "RefCnt": "0",
  "MTU": "0",
  "Window": "0",
  "Gateway": "0.0.0.0",
  "Flags": "0001",
  "IRTT": "0"
 },
 {
  "Use": "0",
  "Iface": "eth1",
  "Metric": "0",
  "Destination": "169.254.0.0",
  "Mask": "255.255.0.0",
  "RefCnt": "0",
  "MTU": "0",
  "Window": "0",
  "Gateway": "0.0.0.0",
  "Flags": "0001",
  "IRTT": "0"
 },
 {
  "Use": "0",
  "Iface": "eth1",
  "Metric": "0",
  "Destination": "192.168.0.0",
  "Mask": "255.255.0.0",
  "RefCnt": "0",
  "MTU": "0",
  "Window": "0",
  "Gateway": "10.226.71.1",
  "Flags": "0003",
  "IRTT": "0"
 },
 {
  "Use": "0",
  "Iface": "eth1",
  "Metric": "0",
  "Destination": "172.16.0.0",
  "Mask": "255.240.0.0",
  "RefCnt": "0",
  "MTU": "0",
  "Window": "0",
  "Gateway": "10.226.71.1",
  "Flags": "0003",
  "IRTT": "0"
 },
 {
  "Use": "0",
  "Iface": "eth1",
  "Metric": "0",
  "Destination": "10.0.0.0",
  "Mask": "255.0.0.0",
  "RefCnt": "0",
  "MTU": "0",
  "Window": "0",
  "Gateway": "10.226.71.1",
  "Flags": "0003",
  "IRTT": "0"
 }
]
ROUTE_FILE = '/proc/net/route'
get_interface_by_ip(ip)[source]

get the interface which can reach to the ip :param ip:

destination ip
Returns:interface name which can reach to the ip. None if failed.
get_interfaces()[source]

get all the interface of this host

get_route_by_ip(ip)[source]

get the route_info which can reach to the ip address :param ip:

destination ip address
Returns:route_info in type of dict
get_routes()[source]

get all the route_info of this host :return: all the readable route_info of this host

Module contents

description:network related module
cup.net.get_local_hostname()[source]

get hostname of this machine

cup.net.get_hostip(hostname=None)[source]

get ipaddr of a host

Parameters:hostname – None, by default, will get localhost ipaddr
cup.net.getip_byinterface(iface='eth0')[source]

get ipaddr of a network adapter

Platform:Linux/Unix

E.g.

import cup
print cup.net.getip_byinterface('eth0')
print cup.net.getip_byinterface('eth1')
print cup.net.getip_byinterface('xgbe0')
cup.net.set_sock_keepalive_linux(sock, after_idle_sec=1, interval_sec=3, max_fails=5)[source]

Set TCP keepalive on an open socket. It activates after 1 second (after_idle_sec) of idleness, then sends a keepalive ping once every 3 seconds (interval_sec), and closes the connection after 5 failed ping (max_fails), or 15 seconds

Parameters:
  • sock – socket
  • after_idle_sec – for TCP_KEEPIDLE. May not work, depends on ur system
  • interval_sec – for TCP_KEEPINTVL
  • max_fails – for TCP_KEEPCNT
cup.net.set_sock_reusable(sock, resuable=True)[source]

set socket reusable

cup.net.set_sock_linger(sock, l_onoff=1, l_linger=0)[source]

set socket linger param (socket.SO_LINGER)

I.g.

sock.setsockopt(
    socket.SOL_SOCKET, socket.SO_LINGER,
    struct.pack('ii', 0, 0)
)
cup.net.set_sock_quickack(sock)[source]

open quickack for the socket

I.g.

sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_QUICKACK, 1)
cup.net.localport_free(port, is_ipv6=False)[source]

judge if a port is used. IPV4, by default

cup.net.port_listened(host, port, is_ipv6=False)[source]

check if the port is being listened on the host