gpt4 book ai didi

powershell - 为什么我的 powershell localhost 服务器这么慢?

转载 作者:行者123 更新时间:2023-12-02 02:43:45 26 4
gpt4 key购买 nike

我拼凑了一个简单的本地服务器来托管纯 HTML 网络应用程序。我需要它,因为某些功能(如 AJAX 请求)无法在直接从文件系统打开的 HTML 中运行。

它看起来像这样,而且确实有效。

# source: https://gist.github.com/19WAS85/5424431

$http = [System.Net.HttpListener]::new()
# Hostname and port to listen on
$http.Prefixes.Add("http://localhost:8080/")
# Start the Http Server
$http.Start()

Add-Type -AssemblyName System.Web
# Log ready message to terminal
if ($http.IsListening) {
write-host "HTTP Server Ready! " -f 'black' -b 'gre'
write-host "$($http.Prefixes)" -f 'y'
#write-host "then try going to $($http.Prefixes)other/path" -f 'y'
}

#New-PSDrive -Name MyPowerShellSite -PSProvider FileSystem -Root $PWD.Path
# INFINTE LOOP
# Used to listen for requests
while ($http.IsListening) {
# Get Request Url
# When a request is made in a web browser the GetContext() method will return a request object
# Our route examples below will use the request object properties to decide how to respond
$context = $http.GetContext()

if ($context.Request.HttpMethod -eq 'GET') {

# We can log the request to the terminal
write-host "$($context.Request.UserHostAddress) => $($context.Request.Url)" -f 'mag'


$URL = $context.Request.Url.LocalPath

# Redirect root to index.html
if($URL -eq "/") {
$URL = "/index.html"
}
$Content = Get-Content -Encoding Byte -Path "web/$URL"
$Context.Response.ContentType = [System.Web.MimeMapping]::GetMimeMapping("web/$URL")
$Context.Response.OutputStream.Write($Content, 0, $Content.Length)
$Context.Response.Close()

}
# powershell will continue looping and listen for new requests...

}

然而,这些请求需要花费大量时间。文件读取和写入输出流的方式有问题。

有解决办法吗?太慢了,基本没用。要尝试它,请确保在存在 web/ 子文件夹的位置运行它。例如,可以通过尝试打开照片来轻松测试低速。

最佳答案

关键是使用 [System.IO.File]::OpenRead 创建一个流,然后将其读取的所有内容直接复制到套接字中,套接字将其发送到浏览器。

这是整个 Powershell 本地目录 HTTP 服务器。如果您想直接提供它运行的目录,请将 "web/$URL" 更改为 $URL:

$http = [System.Net.HttpListener]::new()
# Hostname and port to listen on
$http.Prefixes.Add("http://localhost:8080/")
# Start the Http Server
$http.Start()

Add-Type -AssemblyName System.Web
# Log ready message to terminal
if ($http.IsListening) {
write-host "HTTP Server Ready! " -f 'black' -b 'gre'
write-host "$($http.Prefixes)" -f 'y'
}

# INFINTE LOOP
# Used to listen for requests
while ($http.IsListening) {
# Get Request Url
# When a request is made in a web browser the GetContext() method will return a request object
# Our route examples below will use the request object properties to decide how to respond
$context = $http.GetContext()

if ($context.Request.HttpMethod -eq 'GET') {

# We can log the request to the terminal
write-host "$($context.Request.UserHostAddress) => $($context.Request.Url)" -f 'mag'


$URL = $context.Request.Url.LocalPath

# Redirect root to index.html
if($URL -eq "/") {
$URL = "/index.html"
}

$ContentStream = [System.IO.File]::OpenRead( "web/$URL" );
$Context.Response.ContentType = [System.Web.MimeMapping]::GetMimeMapping("web/$URL")
$ContentStream.CopyTo( $Context.Response.OutputStream );
$Context.Response.Close()
}
# powershell will continue looping and listen for new requests...
}

关于powershell - 为什么我的 powershell localhost 服务器这么慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63131838/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com