Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/netutil.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2014-2019 The Bitcoin Core developers | # Copyright (c) 2014-2019 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Linux network utilities. | """Linux network utilities. | ||||
Roughly based on http://voorloopnul.com/blog/a-python-netstat-in-less-than-100-lines-of-code/ by Ricardo Pascal | Roughly based on http://voorloopnul.com/blog/a-python-netstat-in-less-than-100-lines-of-code/ by Ricardo Pascal | ||||
""" | """ | ||||
import array | import array | ||||
from binascii import unhexlify | from binascii import unhexlify | ||||
from errno import ENOENT, EINVAL | |||||
import os | import os | ||||
import socket | import socket | ||||
import struct | import struct | ||||
import sys | import sys | ||||
# STATE_ESTABLISHED = '01' | # STATE_ESTABLISHED = '01' | ||||
# STATE_SYN_SENT = '02' | # STATE_SYN_SENT = '02' | ||||
# STATE_SYN_RECV = '03' | # STATE_SYN_RECV = '03' | ||||
Show All 9 Lines | |||||
def get_socket_inodes(pid): | def get_socket_inodes(pid): | ||||
''' | ''' | ||||
Get list of socket inodes for process pid. | Get list of socket inodes for process pid. | ||||
''' | ''' | ||||
base = '/proc/{}/fd'.format(pid) | base = '/proc/{}/fd'.format(pid) | ||||
inodes = [] | inodes = [] | ||||
for item in os.listdir(base): | for item in os.listdir(base): | ||||
try: | |||||
target = os.readlink(os.path.join(base, item)) | target = os.readlink(os.path.join(base, item)) | ||||
except OSError as err: | |||||
if err.errno == ENOENT: | |||||
# The file which is gone in the meantime | |||||
continue | |||||
elif err.errno == EINVAL: | |||||
# Not a link | |||||
continue | |||||
else: | |||||
raise | |||||
else: | |||||
if target.startswith('socket:'): | if target.startswith('socket:'): | ||||
inodes.append(int(target[8:-1])) | inodes.append(int(target[8:-1])) | ||||
return inodes | return inodes | ||||
def _remove_empty(array): | def _remove_empty(array): | ||||
return [x for x in array if x != ''] | return [x for x in array if x != ''] | ||||
def _convert_ip_port(array): | def _convert_ip_port(array): | ||||
▲ Show 20 Lines • Show All 123 Lines • Show Last 20 Lines |