Took a look at the other threads but couldn't build anything from it.

I have a list of URL's which I'm trying to get the HEAD status of and check before further manipulation. My current code with httplib works fine (else part), however I need to ensure the code works from behind a proxy, this is where I'm having trouble.

The code works and produces an output (if part), but the output status from the URLs is "400 bad request" which seems to be its not being proxied correctly.

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")
有帮助吗?

解决方案

IIRC, if you're using a proxy server, you have to pass the full URL to the HEAD method...

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()

...otherwise it has no way of knowing which host to connect to.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top