Polipo的Squid风格外部重定向器无法工作

-2 投票
1 回答
1153 浏览
提问于 2025-04-16 14:23

我正在尝试为 polipo HTTP 代理写一个简单的 squid 风格的重定向器,具体内容可以在文档中找到

以下是我的代码:

#!/usr/bin/python

# based on
# http://gofedora.com/how-to-write-custom-redirector-rewritor-plugin-squid-python/

import sys

def modify_url(line):
    l = line.split(" ")
    old_url = l[0]
    new_url = "\n"
    if "experts-exchange" in old_url:
        new_url = "http://127.0.0.1/" + new_url
    return new_url
    
while True:
    line = sys.stdin.readline().strip()
    new_url = modify_url(line)
    sys.stdout.write(new_url)
    sys.stdout.flush()

当我用这个重定向器运行 polipo,并尝试访问http://www.experts-exchange.com/时,我遇到了以下错误:

500 Couldn't test for forbidden URL: Redirector error

实际上,当我尝试访问任何网址时都会出现同样的错误,这让我觉得可能是我的重定向器代码有问题。

在 polipo 的日志中没有提供更多线索,我看到的只有:

Redirector returned incomplete reply.

我哪里做错了?

补充:我已经修复了 modify_url() 函数,使它返回一个值,因为之前没有返回。我仍然遇到同样的错误。

1 个回答

1

那个网站(gofedora)提到,你需要在modify_url这个地方返回一个空行或者修改后的网址,这样才能让它正常工作。如果你手动运行这个程序,也会看到错误信息。解决办法是在modify_url的最后返回new_url。

另外,请注意,你还需要使用chmod +x命令来给你的代理程序授权,这样它才能运行这个脚本。

撰写回答