How to actually download a file through a HTTP / SOCKS4 / SOCKS5 proxy on python?

    2 answers 2

    Here is an example of how you can download a file through socks5 proxy on Python 3 without the monkey-patching socket module:

     #!/usr/bin/env python3 import urllib.request import socks # $ pip install PySocks from sockshandler import SocksiPyHandler # from pysocks socks_port = 9050 # tor socks5 port opener = urllib.request.build_opener( SocksiPyHandler(socks.PROXY_TYPE_SOCKS5, "localhost", socks_port) ) urllib.request.install_opener(opener) #NOTE: global for the process urllib.request.urlretrieve(url, filename) 

    You can not install opener , so that other connections in the same process do not affect, but use it directly: opener.open(url) .


    For http proxies, you can also use build_opener() :

     from urllib.request import ProxyHandler, build_opener opener = build_opener(ProxyHandler(dict(http='host:port'))) 
    • There is no build opener module for urllib3.reqest - Thread
    • Everything worked, it turns out I was stupid - Thread
     import socks import socket import urllib2 socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, proxyHost, proxyPort) socket.socket = socks.socksocket urllib2.urlopen(URL) 

    Like this. Module socks - SocksiPy

    • Instead of SocksiPy I would recommend PySocks as relevant and more doped - andreymal
    • Is that a python? on Linux I sit through a synaptic put on 2 python does not plow - Thread
    • @Thread 2.6, 2.7 How exactly does not work? - Vladimir Martyanov
    • everything already worked, as set by PySocks but now comes the build and ends without error - Thread
    • Everything works, it turns out I'm stupid! - Thread