Make get api requests using Python over Tor

373 views Asked by At

I'm trying to load JSON data through a tor session. URL_1 works fine, but URL_2 does not work (Example I). I don't know why.. Here is the type of error I get:

  • IP: <Response [403]>

I can well recover Json api data without tor session (Example II) but as soon as I introduce the requests.Session() function for URL_2 it no longer works.

Example I

# open Tor browser before
import requests
from stem.control import Controller
from stem import Signal
import json

def get_tor_session():
    # initialize a requests Session
    session = requests.Session()
    # setting the proxy of both http & https to the localhost:9050 
    # this requires a running Tor service in your machine and listening on port 9050 (by default)
    session.proxies = {"http": "socks5://localhost:9150", "https": "socks5://localhost:9150"}
    return session

#url_1 = "https://api.cryptowat.ch/markets/kraken/eurusd/ohlc"
url_2 = 'https://api.1inch.exchange/v1.1/quote?fromTokenSymbol=USDT&toTokenSymbol=KAI&amount=1000000'

s = get_tor_session()
ip = s.get(url_2).text
#ipJ = json.loads(ip)
print(ip)

Example II

import json
url_1 = 'https://api.cryptowat.ch/markets/kraken/eurusd/ohlc'
url_2 = 'https://api.1inch.exchange/v1.1/quote?fromTokenSymbol=USDT&toTokenSymbol=KAI&amount=1000000'
rA = requests.get(url_2) 
jsA = json.loads(rA.content) 
print(jsA)

I have already tried to add a header in the request but it does not work better.

Thank you for your help

0

There are 0 answers