Basics

The following command creates a simple HTTP server from CMD:

@echo off
:main
(type index.html) | nc -w 1 -l -p 80 
goto :main

Here index.html is the file that is displayed to whoever accesses the webpage. It can be viewed by typing localhost in the browser on the same computer or your computer's IP address on any other computer on the network.

The first two lines of index.html must be the following to ensure proper working:

HTTP/1.0 200 OK
Content-Type: text/html

This is the file which I use for testing:

HTTP/1.0 200 OK
Content-Type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii">
        <title>Hi!</title>
    </head>
<body>
    <h1>Hello World!</h1>
    <p>Congrats! Your server is working!</body>
</html>

Advanced

You can also get information via POST requests!

HTML Body:

<body>
    <h1>hello world</h1>
    <form method="post">
    send something: <input type="text" name="postText" />
    <input type="submit" value="Submit" />
    </form>
</body>

And on the backend:

(type files\index.html) | nc -w 1 -l -p 80 | findstr "postText"

However, this just displays the output. In order to fetch the input in a variable so that you can carry out further operations, try this:

(type files\index1.html) | nc -w 1 -l -p 80 | findstr "postText" > par.txt 
for /f "delims== tokens=1,2" %%G in (par.txt) do set %%G=%%H 
        echo User input: %postText%

There is a bug in this code though. Often browsers will send the information multiple times, and will also sometimes send empty requests. To get around this, we can use the following code:

set postPrev=rahul2001BLEH
(type files\index.html) | nc -w 1 -l -p 80 | findstr "postText" > par.txt 
for /f "delims== tokens=1,2" %%G in (par.txt) do set %%G=%%H 
if NOT %postText% == "" (
    if NOT %postText% == %postPrev% (
        echo User input: %postText%
        set postPrev=%postText%
    )
)

This will ignore empty requests and those identical to the previous ones.

Complete Code

server.cmd:

@echo off
set postPrev=rahul2001BLEH
:main
(type files\index.html) | nc -w 1 -l -p 80 | findstr "postText" > par.txt 
for /f "delims== tokens=1,2" %%G in (par.txt) do set %%G=%%H 
if NOT %postText% == "" (
    if NOT %postText% == %postPrev% (
        echo User input: %postText%
        set postPrev=%postText%
    )
)
goto :main

index.html:

HTTP/1.0 200 OK
Content-Type: text/html


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii">
        <title>hello</title>
    </head>
<body>
    <h1>hello world</h1>
    <form method="post">
    send something: <input type="text" name="postText" />
    <input type="submit" value="Submit" />
    </form>
</body>
</html>

Enjoy!

A collection of ported Unix Utilities for Windows

Hi! This repo contains several utilities which are massively useful, for sysadmins, programmers and students.

Go ahead and explore! All utilities are kept up-to-date.

Links: Rahul's Windows Executable Repository; README