I have a vpn connection and when I'm running python -m SimpleHTTPServer, it serves on 0.0.0.0:8000, which means it can be accessed via localhost and via my real ip. I don't want robots to scan me and interested that the server will be accessed only via localhost.
Is it possible?
python -m SimpleHTTPServer 127.0.0.1:8000 # doesn't work.
Any other simple http server which can be executed instantly using the command line is also welcome.
If you read the source you will see that only the port can be overridden on the command line. If you want to change the host it is served on, you will need to implement the
test()
method of theSimpleHTTPServer
andBaseHTTPServer
yourself. But that should be really easy.Here is how you can do it, pretty easily:
And to use it:
As @sberry explained, simply doing it by using the nice
python -m ...
method won't be possible, because the IP address is hardcoded in the implementation of theBaseHttpServer.test
function.A way of doing it from the command line without writing code to a file first would be
If that still counts as a one liner depends on your terminal width ;-) It's certainly not very easy to remember.
In Python versions 3.4 and higher, the
http.server
module accepts abind
parameter.According to the docs: