Wednesday, January 14, 2015

command line http server for windows



I'm looking for a command line tool that acts as simple http "server" in that it can print the requests made to it to the console and reply with 200. In particular, it needs to support POST commands.


Answer



Netcat will do it, if I am understanding you properly.



I like to use the version of netcat from here as it does not have the security issues associated with the -e option.



Now create a text file containing something like:




echo HTTP/1.0 200 OK



hello


hello world




send something:






If you run the following on the command line, where page.txt is the name of your text file above, netcat will act as a one line webserver.



for /l %a in (1,0,2) do type page.txt | nc -w 1 -l -p 80 | findstr "postText"



Connecting to http://localhost will show the web page above. Typing something in the "send something" box and pressing submit will show the value submitted on the command line where you ran netcat. The "-w 1" (disconnect after one second) is a bit of a fudge to get netcat to behave like a web server, but it works, all be it with a one second delay.



You could even use a batch file to return some http which is dependant on what you posted, by redirecting the entire output of the command to the batch file, and using a set /p in a loop, looking for "postText=".


No comments:

Post a Comment