【问题标题】:Creating a http-proxy that can modify the http response before sending it to the client创建一个 http-proxy 可以在将 http 响应发送到客户端之前对其进行修改
【发布时间】:2016-09-05 19:44:34
【问题描述】:

我正在使用wget 从网上获取一些东西,但我不想关注页面的一部分。我想我可以设置一个代理来删除我不想处理的网页部分,然后再将其返回给 wget,但我不确定我将如何完成。

是否有代理可以让我轻松修改 python 或 node.js 中的 http 响应?

【问题讨论】:

  • 我认为需要更多细节。你到底想做什么?修改后你想对响应做什么?您想对响应进行哪些更改?是否要修改标题?还是数据?
  • @Acorn - 我想在代理步骤中更改该数据:Internet -> Proxy -> wget

标签: python http proxy node.js http-proxy


【解决方案1】:

您可以通过多种方式实现这一目标。这应该让你开始(使用 node.js)。在以下示例中,我将获取 google.com 并将所有“google”实例替换为“foobar”。

// package.json file...
{
  "name": "proxy-example",
  "description": "a simple example of modifying response using a proxy",
  "version": "0.0.1",
  "dependencies": {
    "request": "1.9.5"
  }
}

// server.js file...
var http = require("http")
var request = require("request")
var port = process.env.PORT || 8001

http.createServer(function(req, rsp){
  var options = { uri: "http://google.com" }

  request(options, function(err, response, body){
    rsp.writeHead(200)
    rsp.end(body.replace(/google/g, "foobar"))
  })

}).listen(port)

console.log("listening on port " + port)

【讨论】:

  • 谢谢,这真的很有帮助。 :)
【解决方案2】:

在 nodejs 中,我会 fork node-http-proxy 并根据我的需要自定义代码。

恕我直言,这比从头开始编写 http 代理要简单得多。

【讨论】:

    猜你喜欢
    • 2015-09-24
    • 2022-11-04
    • 2018-05-04
    • 2012-05-07
    • 2010-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    相关资源
    最近更新 更多