我需要使用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?
答案 0 :(得分:3)
当您不想使用代理时,可以将套接字设置回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
&gt; 2.10.0您可以在proxies
参数中设置SOCKS proxy。
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))