mySun
mySun

Reputation: 1696

how to disable proxy socks5 in request in python?

I need send requests with socks5 and after send this request, I have send request without seocks5 in python.

For Example:

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))

how to disable proxy socks5 after one request in python?

Upvotes: 1

Views: 2020

Answers (1)

t.m.adam
t.m.adam

Reputation: 15376

You could set the socket back to socket.socket when you don't want to use a proxy.

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))


If you're using requests > 2.10.0 you can set a SOCKS proxy in the proxies parameter.

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))

Upvotes: 3

Related Questions