Advertisement
Guest User

psDummyFileWebServer2.ps1

a guest
Jul 16th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [System.Reflection.Assembly]::LoadWithPartialName("System.Web")
  2. Clear-Host
  3.  
  4. $gigsToDownload =  read-host "In gigabytes, how much data should I serve? (press enter for 4.5) "
  5. $gigsToDownload=[double]$gigsToDownload
  6. if( !($gigsToDownload -is [double] ) -Or $gigsToDownload -le 0) { $gigsToDownload=4.5 }
  7. Write-Host "Will serve "  $gigsToDownload " GB."
  8. $data = [System.Text.Encoding]::UTF8.GetBytes( "".PadRight(16384,[char]0) )
  9.  
  10. $serverAt="http://localhost:80/"
  11.  
  12. $listener = New-Object System.Net.HttpListener
  13. $listener.Prefixes.Add($serverAt)
  14. $listener.Start()
  15. Write-Host "Now ready for requests to: "  $serverAt
  16.  
  17. do {
  18.     $context = $listener.GetContext()
  19.     $response = $context.Response
  20.    
  21.     Write-Host "now serving " $gigsToDownload " GBs of data..."    
  22.  
  23.     $loopTo=[math]::Round( ($gigsToDownload * 1024 * 1024 *1024) / 16384)
  24.    
  25.     $response.ContentType = "application/octet-stream"
  26.     $response.ContentLength64 = $data.Length * $loopTo;
  27.     $response.Headers.Add("Content-Description: File Transfer")
  28.     $response.Headers.Add("Content-Disposition: attachment; filename=dummyFile_" + $gigsToDownload + "GB.bin")
  29.     $response.OutputStream.Flush()
  30.    
  31.     for ($i=0; $i -le $loopTo; $i++) {
  32.         $response.OutputStream.Write($data, 0, $data.Length)
  33.         If ($i % 100 -eq 0) { $response.OutputStream.Flush() }    
  34.         }
  35.    
  36.     $response.Close()
  37.     Write-Host "served data with rCode: " $response.StatusCode
  38. } while ($listener.IsListening)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement