Shell脚本实现的一个简易Web服务器例子分享_linux shell
这篇文章主要介绍了Shell脚本实现的一个简易Web服务器例子分享,本文实现的Web服务器非常简单实用,可以在你不想安装nginx、apache等大型WEB服务器时使用,需要的朋友可以参考下
假设你想测试网页和一些CGI,而你又不想麻烦Apache安装完整的包。这个快速的shell脚本可能只是你所需要的东西。
简而言之,一个web服务器是一个应用程序,该应用程序将本地文本文件通过网络发送给客户的请求。如果你让另一个程序(例如inetd)处理网络情况下,web服务器可以减少到只有 cat "文件名”发送到stdout。当然,困难将提取部分文件名的HTTP请求字符串:任何一个Bash脚本无法轻易做到。
脚本
我们的脚本应该像其他任何脚本一样,加上一些定义:
#!/bin/bash
base=/var/www
inetd将从远程主机接收到的数据传递给我们的脚本,第一行是标准的HTTP请求,后跟零个或更多的头文件。我们记录下请求,并退出休眠:
read request
while /bin/true; do
read header
[ "$header" == $'\r' ] && break;
done
最麻烦的部分:从请求的数据中提取URL并在本地文件中找到对应文件:
url="${request#GET }"
url="${url% HTTP/*}"
filename="$base$url"
返回含有头部信息的文件内容。
if [ -f "$filename" ]; then
echo -e "HTTP/1.1 200 OK\r"
echo -e "Content-Type: `/usr/bin/file -bi \"$filename\"`\r"
echo -e "\r"
cat "$filename"
echo -e "\r"
else
echo -e "HTTP/1.1 404 Not Found\r"
echo -e "Content-Type: text/html\r"
echo -e "\r"
echo -e "404 Not Found\r"
echo -e "Not Found
The requested resource was not found\r"
echo -e "\r"
fi
好了,脚本完了。
安装
为了使它工作,你必须添加以下行到/etc/inetd.conf文件:
www stream tcp nowait nobody /usr/local/bin/webd webd
webd就是你刚刚创建的脚本名称。
使用/etc/init.d/inetd restart使脚本生效后,就可以测试它了。在/var/www下放一些HTML文件,打开你最喜欢的Web浏览器的输入以下地址测试:http://www.php.cn/
请注意,如果你的电脑连接这一个不安全的网络,这个脚本可能不算一个聪明的事,因为谁都可以通过80端口访问你硬盘上的文件。一个更好的办法是使用 tcpd 来保证只允许本地连接。我会写一些相关的信息,发挥你的想象力去干吧!
什么是CGI
就像这个,WEB服务器是没什么用处的,它什么都做不到,而且你可以用其他方法来访问你的文件。我们需要CGI的支持(哪怕是很简单的)。
理论:不通过网络发送一个文本文件,我们运行一个可执行文件,并将其输出。在这之前,我们已经处理的HTTP请求的一步,建立一个 QUERY_STRING变量输出到可执行文件。
要做到这样,你只需要把第三步的代码替换成这个样子的就可以了。
url="${request#GET }"
url="${url% HTTP/*}"
query="${url#*\?}"
url="${url%%\?*}"
filename="$base$url"
if [ "$query" != "$url" -a -x "$filename" ]; then
export QUERY_STRING="$query"
echo -e "HTTP/1.1 200 OK\r"
"$filename"
echo -e "\r"
exit 0
fi
当然,这个小玩意的性能无法与Apache相比,这只是一个小玩意。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to execute .sh file in Linux system? In Linux systems, a .sh file is a file called a Shell script, which is used to execute a series of commands. Executing .sh files is a very common operation. This article will introduce how to execute .sh files in Linux systems and provide specific code examples. Method 1: Use an absolute path to execute a .sh file. To execute a .sh file in a Linux system, you can use an absolute path to specify the location of the file. The following are the specific steps: Open the terminal

An esd file is a compression format used in Windows operating systems, while an ISO file is a disc image file used to create a disc copy or virtual optical drive. When we need to convert esd files to iso files, it may be because ISO files are more commonly used and easier to use. The following will introduce you to some common methods to complete this conversion process. Method 1: Use ESDDecrypter ESDDecrypter is a program specially used to decrypt and convert esd files to iso files.

Linux is an open source, portable, and customizable operating system that is widely used in various fields, such as servers, desktops, embedded devices, etc. The core of Linux is the kernel, which is responsible for managing hardware resources and providing basic services. However, the kernel is not an independent entity and requires a file system to store and access various data and programs. A file system is a method of organizing and managing files. It defines the file's name, location, attributes, permissions and other information. In Linux, there are many different types of file systems, such as ext4, xfs, btrfs, etc., each of which has its own characteristics and advantages. However, among all file systems, there is a special file system, which is the foundation and core of the Linux system, which is

Why can't win7 run bat files? Recently, many users using the Windows7 operating system have reported that they cannot run .bat files. This sparked widespread discussion and confusion. Why can't a well-functioning operating system run a simple .bat file? First, we need to understand the background of the .bat file. A .bat file, also known as a batch file, is a plain text file that contains a series of commands that can be used by the Windows command interpreter (cmd.ex

We've designed this Windows PowerShell scripting tutorial for beginners, whether you're a tech enthusiast or a professional looking to improve your scripting skills. If you have no prior knowledge of PowerShell scripting, this article will start with the basics and be tailored for you. We'll help you master the installation steps for a PowerShell environment and walk you through the main concepts and features of PowerShell scripts. If you're ready to learn more about PowerShell scripting, let's embark on this exciting learning journey together! What is WindowsPowerShell? PowerShell is a hybrid command system developed by Microsoft

If you are an IT administrator or technology expert, you must be aware of the importance of automation. Especially for Windows users, Microsoft PowerShell is one of the best automation tools. Microsoft offers a variety of tools for your automation needs, without the need to install third-party applications. This guide will detail how to leverage PowerShell to automate tasks. What is a PowerShell script? If you have experience using PowerShell, you may have used commands to configure your operating system. A script is a collection of these commands in a .ps1 file. .ps1 files contain scripts executed by PowerShell, such as basic Get-Help

Methods for using URL files to open Internet resources include: double-clicking to open using a web browser. Open it with a text editor, copy the link address and paste it into the browser address bar. Through the command line, use the "start" or "open" command to specify the URL file path. Create a script file that contains the command to open the URL file.

Due to my interest in the Linux operating system and my desire for low-level knowledge, I compiled this article. It serves as a check for basic knowledge and covers all aspects of the system. The tools in the documentation cannot be fully mastered without complete knowledge of computer systems, networks, and operating systems. In addition, system performance analysis and optimization is a long-term series. This document is mainly a comprehensive article compiled by combining the updated Linux performance tuning tool blog post by Brendan Gregg, a Linux guru and Netflix senior performance architect, and collecting articles related to Linux system performance optimization. It mainly explains the principles and performance testing tools involved in conjunction with the blog post. Background knowledge: When analyzing performance issues,
