MoonPoint Support Logo

 

Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals



Advanced Search
November
Sun Mon Tue Wed Thu Fri Sat
         
23
24 25 26 27 28 29 30
2024
Months
NovDec


Sat, Apr 25, 2015 10:41 pm

Using netcat to simulate a web server

If you have a Mac OS X system or a Linux system, you can listen for connections from web browsers on the system by utilizing the netcat utility, which you can run by issuing the nc command from a Terminal window, i.e., a shell prompt. The Terminal application is found in Applications/Utilities on a Mac OS X system.

You won't get the functionality provided by installing web server software, such as the Apache HTTP Server, but you can use the command for testing or to provide one page that is visible to thers from their browsers. As an example, the command below instructs netcat to listen on port 8080 for connections and display the file index.html when the system is accessed on that port. If you pick a port above 1024, such as 8080 rather than 80, you don't need root (admin) privileges in order to have the system listen on the port, but, if you don't use the default port of 80, you need to specify the port used in the URL, e.g., http://www.example.com:80, when you try to connect with a browser.

nc -kl 8080 < index.html

In the above example, two options for netcat, -l and -k are combined into -kl. Their meaning and other options can be displayed by issuing the command nc -h.

     -k      Forces nc to stay listening for another connection after its cur-
             rent connection is completed.  It is an error to use this option
             without the -l option.

     -l      Used to specify that nc should listen for an incoming connection
             rather than initiate a connection to a remote host.  It is an
             error to use this option in conjunction with the -p, -s, or -z
             options.  Additionally, any timeouts specified with the -w option
             are ignored.

If you are running the command on an OS X system, you may see a window appear asking whether you wish to allow netcat to receive incoming connections.

To access the web page from a browser on the system on which you run the command, or from another system, you will need to specify an appropriate IP address for the system, which you can get using the ifconfig -a command, or a fully qualified domain name for it. If the system is accessible from the Internet, you can determine what IP address others need to use by visiting WhatIsMyIP.

E.g., suppose the IP address for the system is 192.168.1.5. That IP address is private IP address space, so wouldn't be accessible from the Internet, but you would substitute whatever IP address others would see for the system, if you had a system that was accessible from the Internet, e.g., whatever address WhatIsMyIP would show if you visited that website using a browser on the system. If you put in http://192.168.1.5:8080, or substituting whatever port number you picked instead of 8080, in a web browser, then you should see the contents of index.html displayed.

If you only need access from a browser on the system itself, then you can just put in the system's localhost, aka, local loopback, address, which is 127.0.0.1. E.g., if you had netcat listen for connections on ort 8080, you could use http://127.0.0.1:8080.

You can use whatever file name you like, e.g., test123.html will work equally as well. If you specified that file name, using http://192.168.1.5:8080 would display the contents of that file without it being specified in the address bar of the browser.

Some browsers may expect the normal status returned by a web server when a page is accessed, so you may want to include the following line at the top of index.html or whatever file you specify. Follow the line with a blank line.

HTTP/1.1 200 OK

E.g., below is a sample index.html file that could be used.

HTTP/1.1 200 OK

<HTML>
<HEAD>
<TITLE>Testing</TITLE>
</HEAD>

<BODY>
<H1 align="Center">Test</H1>
<a href="http://support.moonpoint.com">Moonpoint Support</a><br>
</BODY>

</HTML>

If you don't include the "HTTP/1.1 200 OK" line followed by a blank line, you may see a message such as "The connection was reset" or some other error message, so I would recommend including it.

Until you hit Control-C at the command prompt window, netcat will continue to listen for connections and you will see the HTTP commands issued by browsers. E.g., I might see the following when http://192.168.1.5:8080 is placed in the address bar of Firefox.

GET / HTTP/1.1
Host: 192.168.1.5:8080
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:31.0) Gecko/20100101 Firefox/31.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

GET /favicon.ico HTTP/1.1
Host: 192.168.1.5:8080
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:31.0) Gecko/20100101 Firefox/31.0
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

Note: if the system on which you are running the netcat command is running host-based firewall software, you may need to configure that software to allow incoming connections on the port you specified in the netcat command in order for connectivity to work. E.g., see Opening a firewall port for Firewalld from the command line for the command to use on a CentOS Linux system running FirewallD.

References:

  1. One command line web server on port 80 using nc (netcat)
    Date: August 31, 2011
    commandlinefu.com

[/network/tools/netcat] permanent link

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo