我正在尝试仅使用 Windows 批处理脚本来设置 Web 服务器。
我已经想出了以下脚本:
@echo off
@setlocal enabledelayedexpansion
for /l %%a in (1,0,2) do (
type tempfile.txt | nc -w 1 -l -p 80 | findstr mystring
if !ERRORLEVEL! == 0 (
echo found > tempfile.txt
) else (
echo not-found > tempfile.txt
)
)
但是,响应总是落后于一个请求,我的意思是,如果我在浏览器中输入这样的内容:
REQUEST: localhost/mystring
我会得到以下响应:
RESPONSE: not-found
只有在下一个请求中,我才会收到上述请求的正确答案。
之所以会发生这种情况,是因为一旦 netcat 收到请求,它就会使用尚未根据请求更新的 tempfile.txt 的当前内容进行响应。
有没有什么方法可以阻止响应,直到 tempfile.txt 更新或任何其他方法可以达到预期的结果?
最佳答案
检查-e
选项,你可以编写一个脚本来处理然后执行
nc -L -w1 -p 80 -eexec.bat
这会像您想要的那样将 stdin 和 stdout 从 nc 来回传输到脚本。
exec.bat 可能类似于(有点伪代码):
findstr mystring
if not errorlevel 1 (echo found) else (echo not-found)
或者可能是一个循环(也有点伪代码):
:top
set /p input=
if input wasn't "" echo %input% >> output.dat && goto top
findstr /C:"mystring" output.dat
if not errorlevel 1 (echo found) else (echo not-found)
关于windows - 使用netcat时如何根据请求有条件响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40081779/