本帖最后由 小白龙 于 2024-12-17 23:02 编辑
回复 6# went
感谢大佬分享, 但是完全看不懂代码, 不知如何实现,
我在国外找到了下面的powershell代码, 只有二十来行代码, 就实现了一个简单的http服务器功能, 还能显示图片和网页, 太震撼了,
大佬能把上面提到type参数功能加到这里面吗?- cls
-
- $listener = New-Object System.Net.HttpListener
- $listener.Prefixes.Add("http://localhost:8080/") # Create an HTTP listener
-
- $webRoot = $PSScriptRoot # Specify the website root directory
-
- $listener.Start() # Start the listener
- Write-Host "HTTP server is running at http://localhost:8080/"
- start http://localhost:8080/
-
- while ($true) {
- try {
- $context = $listener.GetContext() # Wait for a request
-
- # Get the request and response objects
- $request = $context.Request
- $response = $context.Response
-
- # Get the file path, default to returning index.html
- $requestedFile = if ($request.Url.AbsolutePath.Substring(1) -eq "") { "index.html" } else { $request.Url.AbsolutePath.Substring(1) }
- $filePath = Join-Path $webRoot $requestedFile
-
- if (Test-Path $filePath) {
- # Determine the file type and set the response
- if ($filePath.EndsWith(".html")) {
- $fileContent = Get-Content $filePath -Raw
- $fileBytes = [System.Text.Encoding]::UTF8.GetBytes($fileContent)
- $response.ContentType = "text/html"
- } elseif ($filePath.EndsWith(".png")) {
- $fileBytes = [System.IO.File]::ReadAllBytes($filePath)
- $response.ContentType = "image/png"
- }
- $response.StatusCode = 200 # Send the file content
- $response.OutputStream.Write($fileBytes, 0, $fileBytes.Length)
- } else {
- $response.StatusCode = 404 # Return a 404 error
- $response.StatusDescription = "Not Found"
- $errorBytes = [System.Text.Encoding]::UTF8.GetBytes("404 - File Not Found")
- $response.OutputStream.Write($errorBytes, 0, $errorBytes.Length)
- }
- } catch {
- $response.StatusCode = 500 # Handle exceptions
- $response.StatusDescription = "Internal Server Error"
- } finally {
- $response.OutputStream.Close() # Close the response stream
- }
- }
-
- $listener.Stop() # Stop the listener
复制代码 index.html页面代码- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Welcome to My PowerShell Server</title>
- <style>
- body {
- font-family: Arial, sans-serif;
- background-color: #f0f0f0;
- color: #333;
- margin: 0;
- padding: 0;
- display: flex;
- justify-content: center;
- align-items: center;
- height: 100vh;
- flex-direction: column; /* 垂直布局 */
- }
- .container {
- text-align: center;
- background-color: white;
- padding: 30px;
- border-radius: 8px;
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
- margin-bottom: 20px; /* 添加底部间距 */
- }
- h1 {
- color: #007BFF;
- }
- p {
- font-size: 18px;
- }
- a {
- color: #007BFF;
- text-decoration: none;
- }
- a:hover {
- text-decoration: underline;
- }
- .image-container {
- text-align: center;
- margin-top: 20px; /* 增加上边距 */
- }
- .image-container img {
- width: 200px; /* You can adjust the size as needed */
- height: auto;
- border-radius: 8px;
- }
- </style>
- </head>
- <body>
-
- <div class="container">
- <h1>Welcome to My PowerShell HTTP Server!</h1>
- <p>This is a simple web page served by a PowerShell script.</p>
- </div>
-
- <div class="image-container">
- <img src="Pic.png" alt="Picture">
- </div>
-
- </body>
- </html>
复制代码
|