Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network connections opened by a process #103

Closed
giampaolo opened this issue May 23, 2014 · 9 comments
Closed

Network connections opened by a process #103

giampaolo opened this issue May 23, 2014 · 9 comments

Comments

@giampaolo
Copy link
Owner

From g.rodola on July 08, 2010 19:31:14

Now that per-process open files has been implemented ( issue 79 ) I'd 
like to do a similar thing for connections/sockets opened by a 
process and then finally stop adding new functionalities and prepare 
to release 0.2.0 version.
Follows an analysis of implementation and api details.

Implementation
==============

On Linux this can be easily done through /proc.
Some code can be reused from r90 , r92 and r95 and adapted to 
whatever API we might want to provide.

As for Windows I have no idea but I'm sure it's not gonna be easy.
We can decide whether not providing this functionality at all or 
reuse r97 which parses netstat output and try to reimplement it in C for 0.2.1 maybe.

On OSX and BSD the simplest way to do this is, again, by parsing lsof 
output, as we did for files.


API
===

I would keep this as simple as possible, providing a single process 
method to list all connection types (TCP, UDP and maybe also Unix sockets).
Process class should grow a get_connections() method returning a list 
of ConnectionInfo object which is going to be similar to 
psutil.CPUInfo: a class which just stores information attributes.
Pseudo example code referring to a process of a browser connected to a site:

>>> import psutil, socket
>>> p = psutil.Process(1254)
>>> p.name 
'firefox'
>>> cons = p.get_connections()
>>> cons
[<ConnectionInfo object at <...>]
>>> cons[0].family
2  # socket.AF_INET 
>>> cons[0].type
1  # socket.SOCK_STREAM
>>> cons[0].local_address
("10.0.0.1", 2568)
>>> cons[0].remote_address
("198.6.52.3", 80)
>>> print cons[0]
ConnectionInfo family=2; type=1, local_address=("10.0.0.1", 2568), 
remote_address=("198.6.52.3", 80)
>>>

Original issue: http://code.google.com/p/psutil/issues/detail?id=103

@giampaolo giampaolo self-assigned this May 23, 2014
@giampaolo
Copy link
Owner Author

From g.rodola on July 09, 2010 09:44:39

Implemented by parsing lsof in r593 for all posix systems including 
Linux which still needs /proc based implementation to be written.
For the returning time I decided to use a namedtuple instead of a 
ConnectionInfo object.
Here's how it looks:

giampaolo@ubuntu:~/svn/psutil$ python
Python 2.7 (trunk:82586, Jul  5 2010, 19:36:29) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> p = psutil.Process(1658)
>>> p.name
'chrome'
>>> for con in p.get_connections():
...     print con
... 
connection(family=2, type=1, local_address=('10.0.0.1', 44080), 
remote_address=('69.63.176.188', 80), status='ESTABLISHED')
connection(family=2, type=1, local_address=('10.0.0.1', 45361), 
remote_address=('72.14.234.83', 443), status='ESTABLISHED')
connection(family=2, type=1, local_address=('10.0.0.1', 47071), 
remote_address=('72.14.234.83', 443), status='ESTABLISHED')
connection(family=2, type=1, local_address=('10.0.0.1', 55504), 
remote_address=('209.85.229.125', 5222), status='ESTABLISHED')
>>>

Labels: -Progress-0in4 Progress-2in4

@giampaolo
Copy link
Owner Author

From g.rodola on July 09, 2010 18:31:47

Linux implementation based on /proc fs committed as r600 .
This implementation is a lot cleaner compared to parsing lsof output 
as it correctly raises AccessDenied every time is needed while lsof 
just returns nothing, as if the process has no active connections.

This basically means that on OSX and BSD we'll have different results 
depending on the privileges of the user who started the python process.

Tomorrow I'll see whether lsof provides an option to avoid the 
suppression of such errors, otherwise we'll have to think about a workaround.
We might write a private C function to call just before lsof, which 
explicitly raises an error in case the user is not supposed to 
succeed in doing certain actions, like task_for_pid() on OSX.

Labels: -Progress-2in4 Progress-3in4

@giampaolo
Copy link
Owner Author

From g.rodola on July 10, 2010 05:38:04

A link for Windows: http://msdn.microsoft.com/en-us/library/Aa366026

@giampaolo
Copy link
Owner Author

From g.rodola on July 10, 2010 06:08:42

Implemented on Windows by parsing netstat output as r601 .
We might decide to release 0.2.0 as is and then think about 
reimplementing this in C for the next release.

Labels: -Progress-3in4 Progress-4in4

@giampaolo
Copy link
Owner Author

From g.rodola on July 10, 2010 06:15:18

Cc: [email protected] wj32.64

@giampaolo
Copy link
Owner Author

From g.rodola on July 10, 2010 06:16:17

Labels: Milestone-0.2.0

@giampaolo
Copy link
Owner Author

From g.rodola on July 19, 2010 13:18:35

The AccessDenied problem described in comment #2 should now been fixed in r607 .
Closing this out as fixed.

@giampaolo
Copy link
Owner Author

From g.rodola on July 19, 2010 13:18:43

Status: Fixed

@giampaolo
Copy link
Owner Author

From g.rodola on March 02, 2013 03:53:48

Updated csets after the SVN -> Mercurial migration: r90 == revision 
1b6f444865db r97 == revision a27959f4d2f9 r593 == revision 
05887139bc4c r600 == revision 3accc16abc02 r601 == revision 
ecbaf7525409 r607 == revision 8ea5c65f2767

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant