F
F
Frunix2021-09-08 20:40:13
Python
Frunix, 2021-09-08 20:40:13

Request to a site using a proxy in Python?

I want to connect to the site using a proxy, but every time it gives me my IP, I tried different options for the guides, but I did not find a solution

import requests
url = "https://httpbin.org/ip";
proxy = "127.0.0.1:8080" #ip для примера
proxies = {"https://" : "https://" + proxy}
print( proxies )
# r = requests.Session()
# r.proxies = proxies
r = requests.get (url, proxies=proxies, timeout=20)
print(r.text)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex F, 2021-09-09
@delvin-fil

Here are two options. The first is through urllib, the second is through requests.

import time
import requests
import urllib.request
import socket
import socks
import locale
import warnings
start_time = time.time()
locale.setlocale(locale.LC_ALL, '')
warnings.filterwarnings('ignore')

url = 'http://ifconfig.io/ip'
socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", port=9050)
socket.socket = socks.socksocket
res = urllib.request.urlopen(url).read()
print (res.decode())

end = round(time.time() - start_time, 2)
print(f'--- {end} seconds ---')
start_time = time.time()

proxy = {
    'http': 'http://111.68.26.237:8080',
    'https': 'https://111.68.26.237:8080'
}

addr = requests.get(url, proxies=proxy).text
print (addr)
end = round(time.time() - start_time, 2)
print(f'--- {end} seconds ---')

Mdya, through TOR is much faster.
209.141.57.164
--- 0.82 seconds ---
111.68.26.237
--- 14.54 seconds ---

U
Uno di Palermo, 2021-09-10
@rogerCopy

with tor browser open:
proxies = {
'http': ' socks5h://127.0.0.1:9150 ',
'https': ' socks5h://127.0.0.1:9150 '
}
session = requests.Session()
r = session .get(page_, proxies=proxies, stream=True)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question