Category: Shell

Quick Test Server

Quick Test Server

Server

    There are times when I need a server in order to test some feature or bit of code. I don’t like spooling up a Linux, Apache, Mysql, and PHP (LAMP) or Linux, Nginx, Mysql, and PHP (LEMP) stack because it’s tedious. Notice what I did there? I made the whole sentence tedious to get you to think it really is tedious. It really isn’t but frankly, I needed a reason to write this. Anyway, so, what is one to do? Well, there are two option that come to mind and those are Python, Php, or Netcat. One might ask, “Whaaat? Rly?” Yup. Really. All one needs to do is open up a terminal/cli and get ta hackin.

    Python call up the Python module SimpleHTTPServer using the switch -m and then give it a port. Make the port greater than 1024 since those are reserved and require root to use. BAM! Open your browser of poison and go to localhost:portNumber or 127.0.0.1:portNumber.

Python Server

python -m SimpleHTTPServer 1337

I did not need to insert any index.html files to the directory. Python automatically gives a list of the directory contents when no index file is found. As a side note, Python doesn’t seem to read Php properly. I have yet to get it to work.

    For Php all one does is use the switch -S and then give it an address (127.0.0.1) and port. Again, make the port greater than 1024 since those are reserved and require root to use. BAM! Once more, open your browser of poison and go to localhost:portNumber or 127.0.0.1:portNumber.

Php Server

php -S 127.0.0.1:1337

It is worth noting that with Php I had to insert an index.php file into the directory I ran the command from. It doesn’t generate any list but does throw an error when no index is found. Additionally, this method seems to only work with Php files. To test if the Php server works, simply insert in the index.php :

<?php
    phpinfo();
?>

    For the last one we will look at Netcat. Netcat is the swiss army knife of the networking tools and has an interesting way of creating a kind of server. To start off, simply create a file called serve.sh. The name is arbitrary but that’s what we will use for this example. Then, in the file add

Netcat Server

#!/bin/bash

    echo "`cat index.html`"

When this is done, simply create an index.html as you would any other. In my case, I did:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Google Link</title>
</head>
<body>
    <h1><a href="http://www.google.com">Google</a></h1>
</body>
</html>

After all this prep, which isn’t much we simply run in the terminal where serve.sh and index.html is:

while true; \
do { \
    echo -e 'HTTP/1.1 200 OK\r\n'; sh serve.sh; \
} | nc -l 1337; \
done

One might need to press enter again to actually run it because of the \’s stating “look to next line for rest of command”. One can also just remove the \’s and put the whole command like so:

while true; do { echo -e 'HTTP/1.1 200 OK\r\n'; sh serve.sh; } | nc -l 1337; done

One can use Php with this by the way. All one does is rename index.html to index.php. Then one adds some code like in the Php example above. After that, in the serve.sh, edit the cat index.php to be php index.php. This has Php interpret the file which then has its output gets echoed back to the requester.
All of this needs some explaining. So what is happening is that the while loop is checking to see if there is anything left to run. Note that the first part before the semicolon tells the browser that there is a server where one requested one. IE, it confirms the request. Then, the serve.sh is ran. In serve.sh it echos out what cat prints from index.html or what Php prints from index.php. This is essentially sent as the file back to the requester. Thus, we can see the h1 sized Google anchor link in this example.

    Voila! A nice juicy server is ready for use in any project that needs one. If one is adventurous, one can use these simple servers to serve files on the local network. To do this, all you have to do is allow the port to accept connections using UFW and then change the address “127.0.0.1” to “0.0.0.0”. This isn’t recommended for long term use but can be useful when needing to transfer something or using an app that’s for the local network. Even then, one might be better off just using ssh or email! Still, in those rare times, all one does is allow the port to be open by using ufw.

ufw allow portNumber/tcp

To remove the rule:

ufw status numbered
ufw delete "the number associated with ones portNumber"
ITDominator’s Github Projects

ITDominator’s Github Projects

Github:
Github Cat Image
    This is my Github account. I have accumulated a good number of programs and repositories over the years. Though, much of it has now been setup in an archive repository since I no longer support the code. The ones not in the archive are my active projects and represent my current skill level barring proper try/catch handling which I am too lazy to implement at this time. Otherwise, they are my projects to improve my coding skills.

    PS – Yes, not all (probably most) properly meet OOP standards. This is a personal choice since I don’t want to create a number of files just to have code split up properly. One controller file suits my needs just fine at this time. Though, one or two will eventually get refactored and setup to meet OOP standards.

Redirect