使用netcat的最小Web服务器 [英] Minimal web server using netcat

查看:766
本文介绍了使用netcat的最小Web服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设置使用netcat的(NC)最小的Web服务器。当浏览器调用了本地主机:1500,例如,它应该表现出一个函数的结果(日期的在下面的例子,但最终这将是一个Python或C程序产生一些数据) 。
我的小netcat的Web服务器必须在bash一会儿真循环,可能是这样简单:

I'm trying to set up a minimal web server using netcat (nc). When the browser calls up localhost:1500, for instance, it should show the result of a function (date in the example below, but eventually it'll be a python or c program that yields some data). My little netcat web server needs to be a while true loop in bash, possibly as simple as this:

while true ; do  echo -e "HTTP/1.1 200 OK\n\n $(date)" | nc -l -p 1500  ; done

当我尝试这个浏览器中显示的那一刻在现有数据NC启动时。我想浏览器中的浏览器请求的那一刻过程中显示的数据,虽然。我怎样才能做到这一点?

When I try this the browser shows the currently available data during the moment when nc starts. I want the browser displays the data during the moment the browser requests it, though. How can I achieve this?

推荐答案

您所面临的问题是,NC不知道什么时候响应该请求。结果
Web会话应该是这样的。

The problem you are facing is that nc does not know when to respond to the request.
A web session should go something like this.

TCP session is established.
Browser Request Header: GET / HTTP/1.1
Browser Request Header: Host: www.google.com
Browser Request Header: \n #Note: Browser is telling Webserver that the request header is complete.
Server Response Header: HTTP/1.1 200 OK
Server Response Header: Content-Type: text/html
Server Response Header: Content-Length: 24
Server Response Header: \n #Note: Webserver is telling browser that response header is complete 
Server Message Body: <html>sample html</html>
Server Message Body: \n #Note: Webserver is telling the browser that the requested resource is finished. 
The server closes the TCP session.

这与开始\\ N仅仅是空行,甚至没有一个空间和包含什么比一个换行符更多的线路。

Lines that begin with "\n" are simply empty lines without even a space and contain nothing more than a new line character.

我有我的bash的httpd由xinetd推出,的xinetd教程。它还记录日期,时间,浏览器的IP地址,以及整个浏览器请求到一个日志文件,并且计算内容长度为服务器头响应

I have my bash httpd launched by xinetd, xinetd tutorial. It also logs date, time, browser IP address, and the entire browser request to a log file, and calculates Content-Length for the Server header response.

user@machine:/usr/local/bin# cat ./bash_httpd
#!/bin/bash
x=0;
Log=$( echo -n "["$(date "+%F %T %Z")"] $REMOTE_HOST ")$(
        while read I[$x] && [ ${#I[$x]} -gt 1 ];do
              echo -n '"'${I[$x]} | sed -e's,.$,",'; let "x = $x + 1";
        done ;
); echo $Log >> /var/log/bash_httpd

Message_Body=$(echo -en '<html>Sample html</html>')
echo -en "HTTP/1.0 200 OK\nContent-Type: text/html\nContent-Length: ${#Message_Body}\n\n$Message_Body"

要添加更多的功能,你可以纳入。

To add more functionality, you could incorporate.

            METHOD=$(echo ${I[0]} |cut -d" " -f1)
            REQUEST=$(echo ${I[0]} |cut -d" " -f2)
            HTTP_VERSION=$(echo ${I[0]} |cut -d" " -f3)
            If METHOD = "GET" ]; then 
                case "$REQUEST" in

                    "/") echo "home page stuff"
                        ;;
                    /who) echo "formatted results of who $(who)"
                        ;;
                    /ps) echo "Formatted results of ps"
                        ;;
                    *) echo "Page not found header and content"
                       ;;
                esac

            fi

扑快乐!

这篇关于使用netcat的最小Web服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆