Ogni tanto può tornare utile per un motivo o per un altro avere a portata di mano la possibilità di spawnare da linea di comando un http static server dalla directory corrente.
Questo è utile ad esempio se si vuole trasferire velocemente un file da un computer ad un altro (o da un computer a più computer). Oppure se si vuole testare velocemente qualcosa relativo ad http (ad esempio un client).
A seconda del sistema su cui ci troviamo, si avrà a disposizione una varietà diversa di linguaggi. Ecco di seguito compilata una lista di comandi che permettono di spawnare un http static server da cli.
Python 2.x
1 | $ python -m SimpleHTTPServer 8000 |
Python 3.x
1 | $ python -m http.server 8000 |
Twisted (Python)
1 | $ twistd -n web -p 8000 --path . |
Oppure:
1 | $ python -c 'from twisted.web.server import Site; from twisted.web.static import File; from twisted.internet import reactor; reactor.listenTCP(8000, Site(File("."))); reactor.run()' |
Ma dipende su Twisted.
Ruby
1 | $ ruby -rwebrick -e'WEBrick::HTTPServer.new(:Port => 8000, :DocumentRoot => Dir.pwd).start' |
Ruby 1.9.2+
1 | $ ruby -run -ehttpd . -p8000 |
Perl
Dipende su Brick.
1 2 | $ cpan HTTP::Server::Brick # install dependency $ perl -MHTTP::Server::Brick -e '$s=HTTP::Server::Brick->new(port=>8000); $s->mount("/"=>{path=>"."}); $s->start' |
Oppure usando Plack:
1 2 | $ cpan Plack # install dependency $ plackup -MPlack::App::Directory -e 'Plack::App::Directory->new(root=>".");' -p 8000 |
http-server (Node.js)
1 2 | $ npm install -g http-server # install dependency $ http-server -p 8000 |
Note: This server does funky things with relative paths. For example, if you have a file /tests/index.html
, it will load index.html
if you go to /test
, but will treat relative paths as if they were coming from /
.
node-static (Node.js)
1 2 | $ npm install -g node-static # install dependency $ static -p 8000 |
No directory listings.
PHP (>= 5.4)
1 | $ php -S 127.0.0.1:8000 |
Erlang
1 | $ erl -s inets -eval 'inets:start(httpd,[{server_name,"NAME"},{document_root, "."},{server_root, "."},{port, 8000},{mime_types,[{"html","text/html"},{"htm","text/html"},{"js","text/javascript"},{"css","text/css"},{"gif","image/gif"},{"jpg","image/jpeg"},{"jpeg","image/jpeg"},{"png","image/png"}]}]).' |
busybox httpd
1 | $ busybox httpd -f -p 8000 |
webfs
1 | $ webfsd -F -p 8000 |
Depends on webfs.
IIS Express
1 | C:\> "C:\Program Files (x86)\IIS Express\iisexpress.exe" /path:C:\MyWeb /port:8000 |
No directory listings. /path
must be an absolute path.
Conclusione
La lista completa ed aggiornata dei comandi per spawnare i server è disponibile su questa pagina.