3

A local server is run using the SimpleHTTPServer module from Python 2.7

$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

Then I use netstat to search for that socket using 8000 as a filter for port number, however, I can't find any socket with the filter (even when I open a browser window and access 127.0.0.1:8000).

$ netstat | grep 8000
// "return Nothing"

Does anyone have ideas about why I can't see the socket binded by Python SimpleHTTPServer in netstat?

Hanfei Sun
  • 45,281
  • 39
  • 129
  • 237

3 Answers3

5

Extending the answer by @MK.


By default, netstat shows only the connected ports, but NOT those which just listening idly. Hence, the confusion.
The author of netstat made the call either for simplicity or for aesthetics.

if you make a connection to that :8000 port, it will show up in just netstat call.

Else, you have to make it explicit that you want it ALL.

netstat -a | grep 8000
kmonsoor
  • 7,600
  • 7
  • 41
  • 55
  • hah, good point. I never even understood the imporatnce of that -a, so used to typing it... – MK. Dec 07 '15 at 13:54
2

Be aware that 0.0.0.0 means that your server will be listing on all IP addresses of your machine, so change the IP address on your python code from 0.0.0.0 to 127.0.0.1 or localhost, if you want to run it locally.

And better run : netstat -an|grep 8000

Iron Fist
  • 10,739
  • 2
  • 18
  • 34
1

When you run netstat w/o the -n argument it will display addresses as names, including protocols. So if you were listening on port 80 it would show up as, say, localhost:http and your grep for 8000 would not find it. In your case it is port 8000 and according to

grep 8000 /etc/services

it probably shows up as localhost:irdmi

Running

netstat -na

will get around that and report the more useful :8000. It is funny how default mode of netstat is both more "friendly" and is almost always less useful than -n.

MK.
  • 33,605
  • 18
  • 74
  • 111
  • imo, this should the "Accepted" answer. btw, who wondering `-n` stands for `--numeric : don't resolve names` and `-a` for `--all: display all sockets (default: connected)` – kmonsoor Dec 07 '15 at 05:46