Anorov/PySocks

Cannot chain handlers?

alexkingnz opened this issue · 0 comments

If I try to chain handlers, things don't work. What am I doing wrong?

  1. With SocksiPyHandler first:
    opener = urllib.request.build_opener(SocksiPyHandler(socks.SOCKS5, "127.0.0.1", 1082), urllib.request.HTTPSHandler(context=myssl))
    opener.handlers
    [<urllib.request.UnknownHandler at 0x7efeddaa0438>,
    <urllib.request.HTTPDefaultErrorHandler at 0x7efeddaa04e0>,
    <urllib.request.HTTPRedirectHandler at 0x7efeddaa05c0>,
    <urllib.request.FTPHandler at 0x7efeddaa0588>,
    <urllib.request.FileHandler at 0x7efeddaa01d0>,
    <urllib.request.DataHandler at 0x7efeddaa0dd8>,
    <sockshandler.SocksiPyHandler at 0x7efeddaa0d68>,
    <urllib.request.HTTPSHandler at 0x7efeddaa0860>,
    <urllib.request.HTTPErrorProcessor at 0x7efeddaa0c88>]
    print(opener.open("https://172.22.255.172"))
    ....
    URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1056)>

So this is successfully using the proxy, but doesn't seem to be using the HTTPS handler which has a context that should ignore the self-signed certificate. My understanding is this is chained in the wrong order.

  1. With SocksiPyHandler second:
    opener = urllib.request.build_opener(urllib.request.HTTPSHandler(contet=myssl),SocksiPyHandler(socks.SOCKS5, "127.0.0.1", 1082))
    opener.handlers
    [<urllib.request.UnknownHandler at 0x7efedda557f0>,
    <urllib.request.HTTPDefaultErrorHandler at 0x7efedda552b0>,
    <urllib.request.HTTPRedirectHandler at 0x7efedda553c8>,
    <urllib.request.FTPHandler at 0x7efedda55be0>,
    <urllib.request.FileHandler at 0x7efedda55cc0>,
    <urllib.request.DataHandler at 0x7efedda55c88>,
    <urllib.request.HTTPSHandler at 0x7efedda55358>,
    <sockshandler.SocksiPyHandler at 0x7efedda55400>,
    <urllib.request.HTTPErrorProcessor at 0x7efedda55c50>]
    print(opener.open("https://172.22.255.172"))
    ...
    URLError: <urlopen error [Errno 110] Connection timed out>

The proxy is not being used, the connection is attempted directly instead of via the proxy, and times out.

How am I supposed to use this? Am I doing something wrong? Could do with more documentation about how to do this.

Note I'm trying to modify someone else's code which builds an opener with several handlers and puts a proxy handler at the end of the chain. And when I put a sockshandler.SocksiPyHandler at the end of the list it is not used, per #2 above.