Python3 - Requests with Sock5 proxy

前端 未结 2 1651
日久生厌
日久生厌 2021-02-08 07:25

Is there a way to use sock5 proxy to use TOR with requests? I know that requests only use http proxy...

import requests
r = requests.get(\'http://www.google.com\         


        
2条回答
  •  -上瘾入骨i
    2021-02-08 08:05

    You can use socks, socket modules

    import socks
    import socket
    from urllib import request
    
    socks.set_default_proxy(socks.SOCKS5, "localhost", 9050)
    socket.socket = socks.socksocket
    r = request.urlopen('http://icanhazip.com')
    print(r.read()) # check ips
    

    The socks package can be installed from multiple packages which are forks of socksipy. One particular one that also works on Python3 is PySocks. You can install it, for example, with pip:

    pip3 install PySocks
    

提交回复
热议问题