当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 运行简单的 HTTP Web 服务器

作者:精品下载站 日期:2024-12-15 00:50:36 浏览:19 分类:玩电脑

使用 PowerShell 运行简单的 HTTP Web 服务器


出于测试目的或作为服务部署阶段的简单存根,我经常需要在 Windows 上运行一个简单的 Web 服务器。为了避免安装全功能 IIS,您可以直接从 PowerShell 控制台运行简单的 HTTP Web 服务器。您可以使用内置的 System.Net.HttpListener .NET 类在任何 TCP 端口上运行此类 Web 服务器。

打开 PowerShell 控制台并创建 HTTP 侦听器:

$httpListener = New-Object System.Net.HttpListener

然后指定要监听的端口。在此示例中,我想在端口 9090 上运行 HTTP Web 服务器。

$httpListener.Prefixes.Add("http://localhost:9090/")
  • 确保该端口未被其他进程使用。您可以找出 Windows 上哪个进程正在侦听 TCP 或 UDP。

  • 为了监听所有网络接口,请使用

    http://+:9090/

    地址。

启动监听器:

$httpListener.Start()

您可以在 HttpListener 对象中使用不同的身份验证类型(Basic、Digest、Windows、Negotiate 或 NTLM)或绑定 SSL 证书来实现 HTTPS。

如果运行此代码,Windows 中会出现一个等待端口 9090 上连接的单独进程。使用以下命令检查它:

nestat -na 9090

或使用 PowerShell 显示开放端口的列表:

Get-NetTCPConnection -State Listen | Select-Object -Property LocalAddress, LocalPort, State | Sort-Object LocalPort |ft

[玩转系统] 使用 PowerShell 运行简单的 HTTP Web 服务器

请记住在 Windows Defender 防火墙中打开此端口。您可以使用PowerShell命令快速创建规则以打开防火墙端口:

New-NetFirewallRule -DisplayName "AllowTestWebServer" -Direction Inbound -Protocol TCP -LocalPort 9090 -Action Allow

现在,在您的硬盘上创建一个文本文件,其中包含您希望网络服务器显示的 HTML。例如:

<!DOCTYPE html>
<html>
<head>
<title>
Lightweight PowerShell Web Server</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {background-color:#ffffff;background-repeat:no-repeat;background-position:top left;background-attachment:fixed;}
h1{font-family:Arial, sans-serif;color:#000000;background-color:#ffffff;}
p {font-family:Georgia, serif;font-size:14px;font-style:normal;font-weight:normal;color:#000000;background-color:#ffffff;}
</style>
</head>
<body>
<h1>Test web page </h1>
<p>This web page was generated by PowerShell using the System.Net.HttpListener class</p>
</body>
</html>

[玩转系统] 使用 PowerShell 运行简单的 HTTP Web 服务器

我使用 UTM-8 编码将此 HTML 代码保存到 C:\PS estwebpage.html。

然后运行以下命令来读取 HTML 文件并将响应发送到用户的浏览器。

$context = $httpListener.GetContext()
$context.Response.StatusCode = 200
$context.Response.ContentType = 'text/HTML'
$WebContent = Get-Content  -Path "C:\PS\testwebpage.html" -Encoding UTF8
$EncodingWebContent = [Text.Encoding]::UTF8.GetBytes($WebContent)
$context.Response.OutputStream.Write($EncodingWebContent , 0, $EncodingWebContent.Length)
$context.Response.Close()

[玩转系统] 使用 PowerShell 运行简单的 HTTP Web 服务器

在浏览器中打开 HTTP 服务器的 URL 地址(

http://localhost:9090

)或使用 PowerShell 获取网页内容。该脚本仅返回一次 HTML 代码,之后您的侦听器将自动停止(仅处理一个用户请求)。

释放 TCP 端口:

$httpListener.Close()

如果您希望 HTTP 服务器继续返回您的页面,则需要将 PowerShell 代码添加到循环中。以下示例以循环方式启动 HTTP 服务器,并在 PowerShell 控制台中按任意键时结束。

write-host "Press any key to stop the HTTP listener after next request"
while (!([console]::KeyAvailable)) {
$context = $httpListener.GetContext()
$context.Response.StatusCode = 200
$context.Response.ContentType = 'text/HTML'
$WebContent = Get-Content -Path "C:\PS\testwebpage.html" -Encoding UTF8
$EncodingWebContent = [Text.Encoding]::UTF8.GetBytes($WebContent)
$context.Response.OutputStream.Write($EncodingWebContent , 0, $EncodingWebContent.Length)
$context.Response.Close()
Write-Output "" # Newline
}
$httpListener.Close()

PowerShell HTTP 服务器将保持活动状态,直到您关闭 PowerShell 控制台或使用以下命令结束会话:

.Close

方法。

您可以将此 PowerShell 脚本作为 Windows 服务运行。

您可以在任何 Windows 主机上运行这样的轻量级 Web 服务器,而无需安装 Internet 信息服务或其他第三方软件。不需要管理员权限。您可以将此 HTTPListener 用作简单的 REST 服务器或通过 HTTP 从远程计算机获取信息。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯