代码之家  ›  专栏  ›  技术社区  ›  mySun

如何在python中禁用请求中的代理socks5?

  •  1
  • mySun  · 技术社区  · 7 年前

    我需要使用socks5发送请求,在发送此请求后,我在python中发送了不带seocks5的请求。

    例如:

    import socks
    import socket
    import requests
    
    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "61.8.75.186", 1080)
    socket.socket = socks.socksocket
    
    # With Socks5
    url = 'http://ip.3300.ir/'
    response = requests.get(url, timeout=10)
    self.logger.info("Set ip: {}".format(response.content))
    
    # Without Socks5
    url = 'http://ip.3300.ir/'
    response = requests.get(url, timeout=10)
    self.logger.info("Set ip: {}".format(response.content))
    

    在python中,如何在一个请求后禁用代理socks5?

    1 回复  |  直到 7 年前
        1
  •  3
  •   t.m.adam    7 年前

    您可以将插座设置回 socket.socket

    import socks
    import socket
    import requests
    
    _socket = socket.socket
    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "61.8.75.186", 1080)
    
    # With Socks5
    socket.socket = socks.socksocket
    url = 'http://ip.3300.ir/'
    response = requests.get(url, timeout=10)
    print("Set ip: {}".format(response.content))
    
    # Without Socks5
    socket.socket = _socket
    url = 'http://ip.3300.ir/'
    response = requests.get(url, timeout=10)
    print("Set ip: {}".format(response.content))
    


    如果您正在使用 requests &燃气轮机;2.10.0您可以设置 SOCKS proxy proxies 参数
    import requests
    
    # With Socks5
    proxies = {'http':'socks5://61.8.75.186:1080', 'https':'socks5://61.8.75.186:1080'}
    url = 'http://ip.3300.ir/'
    response = requests.get(url, timeout=10, proxies=proxies)
    print("Set ip: {}".format(response.content))
    
    # Without Socks5
    url = 'http://ip.3300.ir/'
    response = requests.get(url, timeout=10)
    print("Set ip: {}".format(response.content))