windows - 使用netcat时如何根据请求有条件响应

标签 windows batch-file webserver named-pipes netcat

我正在尝试仅使用 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/

相关文章:

Windows 访问冲突的 C++ 调用堆栈

c++ - Linux 与 Windows 7 (VM) C++ 执行速度

batch-file - 在批处理文件中显示.text的内容?

使用条件 "IF statement"相当于 bash 脚本的 Windows 批处理

Swazoo 和 Komanche 之间的性能差异?

windows - Windows 的系统变量和环境变量有什么区别?

linux - .htaccess 和 dns 将 windows hosted .com 网站重定向到我的 linux .co.za wordpress 网站

batch-file - 从任务计划程序启动批处理文件时是否可以显示命令提示符窗口?

java - 我如何根据 GPS 位置在网络服务器和 Android 应用程序之间进行交互?

linux - EXT4 是否已过时用于生产?