带代理的wxpython httplib显示400个错误请求

时间:2013-04-17 13:51:30

标签: python wxpython httplib

看看其他线程,但无法从中构建任何东西。

我有一个URL列表,我正在尝试获取HEAD状态并在进一步操作之前检查。我使用httplib的当前代码工作正常(否则部分),但我需要确保代码在代理后面工作,这是我遇到麻烦的地方。

代码工作并产生一个输出(如果是部分),但URL的输出状态是“400 bad request”,这似乎是它没有被正确代理。

dlg = wx.MessageDialog(self, "Are you connected to a proxy?", "LAN check", wx.YES | wx.NO | wx.ICON_INFORMATION)
    if dlg.ShowModal() == wx.ID_YES:
        self.urlFld.SetValue("")
        for line in self.myList:
            url = urlparse.urlparse(line)
            conn = httplib.HTTPConnection("myproxy.com", 8080)
            conn.request("HEAD", url.path)
            r1 = conn.getresponse()
            r1 = r1.status, r1.reason
            r1 = str(r1)
            self.urlFld.AppendText(url.scheme + "://" + url.hostname + url.path + "\t\t\t" + r1 + "\r")
    else:
        self.urlFld.SetValue("")
        for line in self.myList:
                url = urlparse.urlparse(line)
                conn = httplib.HTTPConnection(url.hostname)
                conn.request("HEAD", url.path)
                r1 = conn.getresponse()
                r1 = r1.status, r1.reason
                r1 = str(r1)
                self.urlFld.AppendText(url.scheme + "://" + url.hostname + url.path + "\t\t\t" + r1 + "\r")

1 个答案:

答案 0 :(得分:1)

IIRC,如果您使用的是代理服务器,则必须将完整的网址传递给HEAD方法......

if dlg.ShowModal() == wx.ID_YES:
    self.urlFld.SetValue("")
    for line in self.myList:
        conn = httplib.HTTPConnection("myproxy.com", 8080)
        conn.request("HEAD", line)
        r1 = conn.getresponse()

...否则无法知道要连接的主机。