cup.net package

Submodules

cup.net.route module

author:YangGuang yangguang07@baidu.com
create_date:2014
last_date:2014
descrition: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

author:Guannan Ma
create_date:2014
last_date:2014
descrition:network related module
cup.net.get_local_hostname()[source]

获得当前机器的hostname

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

拿到某hostname的ip地址.

Parameters:hostname – 机器的hostname, 默认为None代表不传值, 函数将自动获取当前机器的ip
cup.net.getip_byinterface(iface='eth0')[source]
@platform:
Linux/Unix

获得某个网卡的ip地址 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
  • interval_sec – for TCP_KEEPINTVL
  • max_fails – for TCP_KEEPCNT
cup.net.set_sock_reusable(sock, resuable=True)[source]

设置socket的端口是否可被重复使用, 默认resuable==True

cup.net.set_sock_linger(sock)[source]

关闭socket的linger参数。 实际产生的效果如下:

I.g.

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

打开socket的quickack功能:

I.g.

sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_QUICKACK, 1)

Table Of Contents

Previous topic

cup package

Next topic

cup.net.async package

This Page