faqts : Computers : Programming : Languages : Python : Common Problems

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 8 people (38%) answered Yes
Recently 2 of 7 people (29%) answered Yes

Entry

What Python code can I use to find all the network ports that are in use on a server?

Aug 15th, 2002 23:22
Simon Ryan, unknown unknown, Aaron Berg


to figure out what ports are used on a server you could use netstat -a 
-n
import os 
popen("netstat -a -n")
----
Alternatively if you don't want to fork another process and you have
access to a /proc filesystem you can try opening the following:
    /proc/net/tcp
    /proc/net/udp
There will be one line per port and the details will be in hex of course.
eg: Sendmail on my system looks like:
   9: 00000000:0019 000...(remaining tokens removed for clarity)
It is the ninth port in the list, it is bound to all interfaces
(00000000) and it is port 25 which is 0x0019 hexadecimal