Sid
Sid

Reputation: 4055

How to check the header being sent out with a python request?

Here is the code I am using:

def get_tor_session():
    os.startfile(r"C:\Users\Downloads\tor-win32-0.2.9.9\Tor\tor.exe")

    session = requests.session()
    # Tor uses the 9050 port as the default socks port
    session.proxies = {'http':  'socks5://127.0.0.1:9050',
                   'https': 'socks5://127.0.0.1:9050'}
    return session

url = 'https://www.nseindia.com/marketinfo/companyTracker/mtOptionKeys.jsp?companySymbol=ACC&indexSymbol=NIFTY&series=EQ&instrument=OPTSTK&date=-'

session = get_tor_session()    
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1)      AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
raw_page = session.get(url, headers=headers).text

I keep getting a: 403 forbidden response from the url. When I make a get request without tor, there is no problem.

I tried:

>>>print(session.headers)

 {'User-Agent': 'python-requests/2.10.0', 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'keep-alive'}

Trying to figure out if the headers added above is the one being sent out or the one that was printed with the command at the end.

Upvotes: 0

Views: 1213

Answers (1)

Arount
Arount

Reputation: 10403

You need to access to sent_request.request.headers:

import requests

r = requests.get('http://www.url.foo')
print(r.request.headers)

Output:

{'User-Agent': 'python-requests/2.18.4', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}

Upvotes: 1

Related Questions