129

I am using this command on Ubuntu but it is starting on port 8080 and I don't have another server running so I'd like it to start on port 80. I saw ways that you could set up a bash script to do something like this, but isn't there a command line flag or something simpler to specify the port?

python -m SimpleHTTPServer
derobert
  • 109,670
cwd
  • 45,389
  • 4
    Are you running as root? You can't listen on a port <1024 unless you have root privileges - and I would strongly advise against that. – Nathan Osman Nov 14 '11 at 19:53

2 Answers2

227

sudo python -m SimpleHTTPServer 80

for python 3.x version, you may need :

sudo python -m http.server 80

Ports below 1024 require root privileges.

As George added in a comment, running this command as root is not a good idea - it opens up all kinds of security vulnerabilities.

However, it answers the question.

jrg
  • 4,316
  • 8
    I might also add that running this command as root is not a good idea - it opens up all kinds of security vulnerabilities. – Nathan Osman Nov 14 '11 at 19:56
  • Very nice, and good to know. But why is it that ports less than 1024 require root? Because they are used for common things like FTP and SSH? BTW - I was using this temporarily to test firewall rules - worked very well. – cwd Nov 14 '11 at 20:14
  • 2
    @cwd :) The TCP/IP port numbers below 1024 are special in that normal users are not allowed to run servers on them. This is a security feaure, in that if you connect to a service on one of these ports you can be fairly sure that you have the real thing, and not a fake which some hacker has put up for you. – jrg Nov 14 '11 at 20:17
  • 4
    @cwd If you want to run the server on a port <1024, use iptables to route incoming connections to a port ≥1024. For testing, do something like sudo nc -l 80 (netcat). And see Why are the first 1024 ports restricted to the root user only? – Gilles 'SO- stop being evil' Nov 14 '11 at 23:42
  • 1
    @jrg A port <1024 only matters if you trust the sysadmin on the server but not other users. It's near-userless outside a few local networks nowadays: most servers aren't multiuser machines. – Gilles 'SO- stop being evil' Nov 14 '11 at 23:43
  • 3
    This is a related question about how to drop privileges once you've opened port 80. – jcollado Nov 21 '11 at 22:43
  • Yay. I was just looking for how to use a custom port with SimpleHTTPServer from the command line. – Jaanus Sep 10 '14 at 16:22
  • is it possible to call this in a py2x and py3x compatible way? I tried using the six library but it doesnt seem to work with: 'python -m six.moves.SimpleHTTPServer' – fersarr Oct 11 '18 at 13:11
  • The command above for python 3.x should be sudo python3 -m http.server 80 – Clayton Dukes Oct 21 '21 at 23:57
6

Do something like :

python -m SimpleHTTPServer 50505

Where 50505 is the port number.

Prvt_Yadav
  • 5,882