My Test File

Simple command line HTTP server

I have a script which generates a daily report which I want to serve to the so called general public. The problem is I don’t want to add to my headaches maintance of a HTTP server (e.g. Apache) with all the configurations and security implications. Is there a dead simple solution for serving one small HTML page without the effort of configuring a full blown HTTP server?

Indeed! Use FTP, like the World Wide Web did before HTTP was finished! (Although I suppose it wasn’t quite as world-wide in early 1990s. 😉)

22 Answers 22

python -m SimpleHTTPServer # or the Python 3 equivalent python3 -m http.server 

It should will serve whatever’s in the CWD (e.g. index.html) at http://0.0.0.0:8000.

You can optionally specify a port number like this: python3 -m http.server 1337 . You can’t specify which IP to bind to as far as I can tell. Note: To listen to port 80 you must have root privileges, e.g.: sudo python3 -m http.server 80

This one is nice but it has an issue with redirecting to an URL with a trailing slash added. That’s why I prefer the twistd version: twistd -n web -p 8000 —path .

To change default listening port 8080 (with python2) to something else, just put a port number after: python -m SimpleHTTPServer 3000

  1. serve static files using your current directory (or a specified directory) as the server root
  2. be able to be run with a single, one line command (dependencies are fine if they’re a one-time thing)
  3. serve basic file types (html, css, js, images) with proper mime types, require no configuration (from files or otherwise) beyond the command itself (no framework-specific servers, etc)
  4. must run, or have a mode where it can run, in the foreground (i.e. no daemons)
erl -s inets -eval 'inets:start(httpd,[,,,,,,,,,,,]>]).' 
cpan Plack plackup -MPlack::App::Directory -e 'Plack::App::Directory->new(root=>".");' -p 8000 

Use node.js , fast and lightweight.

just use simple nc netcat command to start a quick webserver on a port and serve the content of a file including the server response headers.

A bare node.js process with only the interactive shell running takes 15MB (7.5 is shared) of RAM. And then you have to run the HTTP server inside it. It is funny that people see it as lightweight. 😉

yeah I consider it light weight, you can scale well with such less memory footprint. Please read thecodinghumanist.com/blog/archives/2011/5/6/… However, if you find it cumbersome to use node.js, then the simple netcat utility serves the short lived purpose well.

You are of course right if you compare node with Apache but what I found amusing was how node looks when compared to cr.yp.to/publicfile.html or something similar. 🙂

+1 for the nc based solution :). Note that the -ne flags for echo may not be portable, using the printf command instead may be a better alternative.

Chrome won’t stop throbbing unless you use the second nc command. Also, not both of the nc commands show will quit after responding. You can keep restarting it with: while :; do < echo -ne "HTTP/1.0 200 OK\r\nContent-Length: $(wc -c | nc -l -p 8080; done . Use ctrl+z to but it to sleep.

Since version 5.4.0 PHP also has a built-in web server:

You can Specify the web server’s documents directory with -t , for example:

php -S localhost:8000 -t /var/lib/www 

If you want to be able to access the server over the network then:

php -S 0.0.0.0:8000 -t /var/lib/www 
gcc -O -DLINUX nweb.c -o nweb 

It works well, but 2 comments for users: 1) it serves index.html from the provided directory, 2) from nweb —help : «Not Supported: URLs including «..», Java, Javascript, CGI». So, for example, Live.js to automatically refresh the page on file update does not work.

That’s right, but you actually just need to add the wanted extensions to the list of mime types for the files to be served. Same for index.html, you can adjust that very easely.

Node has a simple, fast, light HTTP server module. To install:

sudo npm install http-server -g 

(Assuming you have node and npm already installed.)

To run it, using the current directory as the website root:

This creates a server on http://0.0.0.0:8080/ .

It works. I have a Node project on a FreeBSD machine, I just run npm install -D http-server inside project directory and then add the following lines to my package.json file: «scripts»: < "build": "webpack && cd src/public && http-server" >, now I just need to run npm run build on project directory to start the HTTP server, listening on port 8080 by default.

First I tried » python3 -m http.server «, but it’s single-threaded and only 1 client can download at a time, the others need to wait. This solution with Node.js works better, due to the async nature of Node. If you want to share a file with several people, use this.

A simple fix/enhancement to a slightly unfairly (imho) down voted answer might also work. Let’s set up the html file first .

echo '

OK!

' > my_file.html

(Thx to Steve Folly for catching my typo in the HTML above. fixed.)

Now you can serve it up with this one-liner:

while true; do echo -e "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" | cat - my_file.html | nc -l -p 8080; done 

This basic idea lends itself to other tricks that might work for you via other cat or subshell ideas such as:

while true; do echo -e "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nI think the date is $(date), Have a good day!" | nc -l -p 8080; done 

Simple Ruby one liner to serve a directory:

Try using SimpleHTTPServer in Python.

mkdir ~/public_html command_to_generate_output > ~/public_html/output.txt (cd ~/public_html; python -c 'import SimpleHTTPServer,BaseHTTPServer; BaseHTTPServer.HTTPServer(("", 8080), SimpleHTTPServer.SimpleHTTPRequestHandler).serve_forever()') 

The first two lines are setup for the web server. The last line creates a simple web server, opened on port 8080, which only serves files from ~/public_html . If only one file is in that directory, then only that is exposed: http://localhost:8080/output.txt .

that’s what i use, just copy the lastline and paste it on a .py file, then run it with python (or make it executable). Keep in mind that you have to run with python 2.x interpreter.

Another option would be to install lighttpd. Following are suggested steps to install lighttpd on a Unbuntu 12.04 LTS.

apt-get update apt-get upgrade --show-upgraded apt-get install lighttpd ifconfig http://[your-ip-address]:80 /etc/lighttpd/lighttpd.conf (Edit to add server.port) server.port = "8080" 

Note: Documentroot is where all web accessible files will be places. The location is /var/wwww

The above step will install a basic lighttpd web server. For more information refer the following references

Источник

6 ways to start a local HTTP server quickly

As a developer, you usually have to spin up a simple web server from a local folder in your system. Configuring and running a full blown web server like Nginx or Apache simply takes too much time, so much that it will make your workflow completely unproductive.

In this article, you’ll find quick ways to start a local HTTP server, depending on your operating system and programming language you’re working with.

Mongoose – instant local HTTP Server

image-20210617112933189

Mongoose is a event-driven non-blocking APIs networking library for C/C++. The library has been actively developed since 2004, used by vast number of open source and commercial products, mostly embedded platform.

While being a networking library itself, Mongoose homepage also host binaries for Windows, MacOS and Linux that allows for quick server start up.

Mongoose executable does not depend on any external library or configuration. This makes Mongoose perfect for all kinds of demos, tests, file sharing, and API testing.

In order to start a server, you only need to drop its executable into a folder/directory and run it. The server will be instantly started on port 8080.

Also, if you need to install the HTTP server as a service, look no further, Mongoose has that feature integrated.

image-20210617161919426

If you need more options, you can configure it by creating a mongoose.conf file with respective options can be created in the same directory where executable lives. The configuration details can be found at Mongoose homepage.

Nginx – the big name but tiny-sized

Nginx logo

Believe it or not, despite being a big name on the market, Nginx entire build weights only under 2MBs in size, which makes it eligible for the “lightweight” category of web servers.

If you’re using Windows, just head over to nginx download page and get the zip package. Once extracted, double clicking nginx.exe will immediately spin up a local HTTP server at port 80.

Node.js local HTTP server

If you have Node.js installed, you can use npm to get an additional http-server package to quickly spin up a local HTTP server.

npm install -g http-serverCode language: Bash (bash)

Once the installation is done, spinning up a local HTTP is super easy. You just have to run http-server inside the folder you want to expose.

By default, http-server will listen on port 8080, but you can change it using the -p flag if you want (see more options by running http-server —help ).

Python local HTTP server

If you’re using Ubuntu or Debian, there’s a high chance you already had Python 3 installed.

In this case, you can use Python’s http standard library to spin up a local HTTP server by running the following command inside any directory you want to expose.

python -m http.server 8080Code language: Python (python)

Feel free to replace 8080 with any other port of your choice.

Chrome-based local HTTP server

files/images/200OK.PNG

Google Chrome is now the most popular browser in the world. Its functionality can be extended through extensions.

There is an extension named Web Server for Chrome that helps you spin up a simple local HTTP server to serve static files and HTML content.

In addition to listening to connections at certain port, the Chrome-based local HTTP server can also runs in the background and prevent computer from sleeping, which makes it perfect for small development tasks.

image-20210617153724130

Web Server for Chrome can be installed on all Chromium-based browsers which supports extensions. Once installed, it can either be opened by accessing chrome://apps/ from browser, or Windows Settings > Chrome Apps > Web Server for Chrome.

Simple web-server – tiny HTTP server with a GUI

image-20210617155425644

Simple web-server is a small application that expose a folder to a local HTTP server.

It only works in Windows and weights only 330 KB, which makes it perfect for serving HTML sites on the go.

Simple web-server works right out of the box without administrative privileges. Uninstalling the app can simply be done by deleting the executable.

Although the tool doesn’t integrate a help manual, all of its features are highly intuitive and you can easily get an idea of how it works.

Источник

Читайте также:  Настроить интернет линукс минт
Оцените статью
Adblock
detector