如何让我的本地代理服务器将html返回给浏览器?

时间:2021-01-08 07:37:48

I have managed to get a proxy server running. It currently only sends hardcoded GET requests but that is fine for the time being. The problem is that I don't know how I get the data I receive from the webserver to show up in my browser. I can however print the body of the data in my terminal without any problem. An unrelated problem is also that the returned data is a "302 Moved" error. I'm very thankfull for any help!

我设法让代理服务器运行。它目前只发送硬编码的GET请求,但暂时没有问题。问题是我不知道如何从网络服务器收到的数据显示在我的浏览器中。但是,我可以毫无问题地在终端中打印数据正文。一个不相关的问题是返回的数据是“302 Moved”错误。我非常感谢任何帮助!

require 'socket'

def handle_request(client, host, port, path)
  puts "0"
  socket = TCPSocket.open(host, port)
  request = "GET #{path} HTTP/1.0\r\n\r\n"

  socket = TCPSocket.open(host,port)
  socket.print(request)
  response = socket.read

  headers,body = response.split("\r\n\r\n", 2) 
  client.puts body
  puts body

  socket.close
end



server = TCPServer.open(2000)
loop{
  client = server.accept
  host = "www.google.se"
  port = 80
  path = "/index.html"
  handle_request(client, host, port, path)

  client.close
}

1 个解决方案

#1


0  

Just run your program and point your browser at http://localhost:2000.

只需运行程序并将浏览器指向http:// localhost:2000即可。

#1


0  

Just run your program and point your browser at http://localhost:2000.

只需运行程序并将浏览器指向http:// localhost:2000即可。