• caglararli@hotmail.com
  • 05386281520

Website doesn’t provide cookie (csrf token) when connecting over proxy. Why/how?

Çağlar Arlı      -    65 Views

Website doesn’t provide cookie (csrf token) when connecting over proxy. Why/how?

The Python code below should grab a CSRF token cookie from Instagram.com, but I get an error message when running the program through any sort of HTTPS or SOCKS5 proxy, informing me that the CSRF cookie isn't provided.

If s.proxies is commented out so that the program isn't being run via a proxy, it works just fine without error, and Instagram.com provides a CSRF token cookie.

So my question is: how does Instagram detect that I'm running the program from a proxy? In other words, how does it differentiate between a normal request and a request running through a proxy?

import requesocks
import requests

s = requesocks.session()
# Tor uses the 9050 port as the default socks port
s.proxies = {'https': 'socks5://127.0.0.1:9050'}
url = 'https://www.instagram.com/accounts/web_create_ajax/'
s.get(url)
tok = s.cookies['csrftoken']

#Outputs: 
#Traceback (most recent call last):
#File "tor.py", line 24, in <module>
#tok = s.cookies['csrftoken']
#KeyError: 'csrftoken