forked from openucx/ucx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request openucx#31 from TomAugspurger/default-address
REF/ENH: Add function for getting the address of an interface
- Loading branch information
Showing
15 changed files
with
91 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import ucp_py as ucp | ||
|
||
|
||
def test_get_address(): | ||
result = ucp.get_address() | ||
assert isinstance(result, str) | ||
assert '.' in result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from ._utils import get_address, sizeof | ||
from ._libs.ucp_py import * |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import fcntl | ||
import socket | ||
import struct | ||
import sys | ||
|
||
|
||
def get_address(ifname='ib0'): | ||
""" | ||
Get the address associated with a network interface. | ||
Parameters | ||
---------- | ||
ifname : str, default ib0 | ||
The network interface name to find the address for. | ||
By default, 'ib0' is used. An OSError is raised for | ||
invalid interfaces. | ||
Returns | ||
------- | ||
address : str | ||
The inet addr associated with an interface. | ||
Examples | ||
-------- | ||
>>> get_address() | ||
'10.33.225.160' | ||
>>> get_address(ifname='lo') | ||
'127.0.0.1' | ||
""" | ||
# https://stackoverflow.com/a/24196955/1889400 | ||
ifname = ifname.encode() | ||
|
||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
return socket.inet_ntoa(fcntl.ioctl( | ||
s.fileno(), | ||
0x8915, # SIOCGIFADDR | ||
struct.pack('256s', ifname[:15]) | ||
)[20:24]) | ||
|
||
|
||
def sizeof(obj): | ||
""" | ||
Get the size of an object, defined as | ||
1. it's length | ||
2. it's Python overhead | ||
So this is appropriate for sequences where each element | ||
is a byte (bytestrings or memoryviews). | ||
""" | ||
return len(obj) + sys.getsizeof(obj[:0]) |