question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Win10] psutil.net_connections return mixed object types (namedtuple and tuple)

See original GitHub issue

Platform

  • sys.getwindowsversion(major=6, minor=2, build=9200, platform=2, service_pack=‘’)
  • ‘2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)]’
  • Version: 5.6.2

Bug description psutil.net_connections return mixed object types (namedtuple and tuple) I think it should be return type with namedtuple instead of tuple,or mixed types.

print [sconn.raddr for sconn in sconn_list if sconn.raddr.port == 993 and sconn.status == 'ESTABLISHED']
import psutil

sconn_list = psutil.net_connections(kind='tcp')

print sconn_list[0].raddr.port
print [type(sconn.raddr) for sconn in sconn_list]
print [sconn.raddr for sconn in sconn_list if sconn.raddr.port == 993 and sconn.status == 'ESTABLISHED']

Test results

Traceback (most recent call last):
  File "C:/Users/Guodong/PycharmProjects/LinuxBashShellScriptForOps/functions/net/tcp/port/check-port-connection.py", line 33, in <module>
    print [sconn.raddr for sconn in sconn_list if sconn.raddr.port == 993 and sconn.status == 'ESTABLISHED']
AttributeError: 'tuple' object has no attribute 'port'

thanks.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
giampaolocommented, May 29, 2019

Hang on. =) If you see a tuple instead of namedtuple then it’s a bug.

0reactions
DingGuodongcommented, May 29, 2019

😃, OK, you are so nice that I very appreciate it.

I am using this code to avoid this issue.

new code:

my_sconn_list = [sconn for sconn in sconn_list if sconn.status == 'ESTABLISHED' and sconn.raddr.port == port]

old code:

from psutil._common import addr
my_sconn_list = [sconn for sconn in sconn_list if
                 isinstance(sconn.raddr,
                            addr) and sconn.raddr.port == port and sconn.status == 'ESTABLISHED']

I think the location of sconn.status == 'ESTABLISHED' is key.

Thanks~~~

Read more comments on GitHub >

github_iconTop Results From Across the Web

psutil.net_connections() should return namedtuple, but it is not ...
The documentation says: raddr : the remote address as a (ip, port) named tuple or an absolute path in case of UNIX sockets....
Read more >
Write Pythonic and Clean Code With namedtuple - Real Python
With namedtuple() , you can create immutable sequence types that allow you to access their values using descriptive field names and the dot...
Read more >
Namedtuple in Python - GeeksforGeeks
Python supports a type of container dictionaries called “namedtuple()” present in ... using ** operator to return namedtuple from dictionary.
Read more >
NamedTuple class declaration for Python 3.6 not supported by ...
Pycharm doesn't recognize the type of the instance. Checked collection.namedtuple subclass, it's subclass shows no parameter info at all. (Doesn't complain like ...
Read more >
Inheritance for Python Namedtuples - Zecong Hu's blog
Create a namedtuple class using _make_nmtuple . Note that the returned namedtuple class does not support default values or contain type ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found