“Ad-hoc webserver” for static files on UNIX/MacOSX

2019-03-08 23:57发布

Is there such a thing as a tiny little webserver that I can invoke from the command line that just fetches files from the local filesystem and serves them via HTTP on specific port?

I'd like to be able to do something like this:

$ cd ~/Sites/mysite
$ serve . 10.0.1.1 8080

This should fire up a webserver that listens on 10.0.1.1:8080 and serves files from the current directory (".") – no PHP, ASP or any of that needed.

Any suggestion greatly appreciated.

4条回答
Fickle 薄情
2楼-- · 2019-03-09 00:06

Apache HTTPD is built into Mac OS X - just switch on 'Web Sharing' in the Sharing Preferences.

To make it also work over port 8080, you'd need to add some configuration. See this article on Serverfault for starting point.

查看更多
Evening l夕情丶
3楼-- · 2019-03-09 00:21

$ python -m SimpleHTTPServer [port]

will start a webserver in the current directory serving whatever files are found there.

In a few cases this won't work well, for example the server is single-threaded (so no simultaneous downloads) and doesn't handle byte-range requests (clients expecting Range: support often fail badly).

查看更多
狗以群分
4楼-- · 2019-03-09 00:28

If you have python installed:

$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
查看更多
Anthone
5楼-- · 2019-03-09 00:30

Python3 can serve the current directory via HTTP using http.server:

$ python3 -m http.server

Where

  • python3 the current version of python
  • -m stands for module
  • http the http package
  • http.server the server module (of the http package)

Per default, http.server listens on port 8000, but you can specify another like this:

$ python3 -m http.server 8080
查看更多
登录 后发表回答