How should I specify cookies for an HTTP protocol open request? #918
-
I am looking to create a Pangeo Forge recipe for data hosted on NCAR RDA, which requires a session login to access the files via HTTP. However, I am failing to execute said recipe due to receiving a 403 Forbidden error on fsspec's attempt to open the HTTP file (which arises during a A minimal reproducible example comparing direct usage of import fsspec
import getpass
import requests
# Get a login session to have proper cookies
username = input("Enter RDA Username: ")
userpass = getpass.getpass("Enter RDA Password: ")
url = 'https://rda.ucar.edu/cgi-bin/login'
values = {'email': username, 'passwd': userpass, 'action': 'login'}
# Authenticate
login = requests.post(url, data=values)
if login.status_code != 200:
print('Bad Authentication')
print(login.text)
else:
requests_kwargs = {'cookies': login.cookies, 'allow_redirects': False}
# Configuration
filename = 'https://rda.ucar.edu/data/OS/ds841.6/volumes/2010/20100120/nexrad_3d_v4_2_20100120T180500Z.nc'
file_base = filename.split('/')[-1]
chunk_size=1048576 This works: req = requests.get(filename, stream=True, **requests_kwargs)
with open(file_base, 'wb') as outfile:
for chunk in req.iter_content(chunk_size=chunk_size):
outfile.write(chunk) This fails remote_file = fsspec.open(filename, **requests_kwargs)
with remote_file as infile:
with open(file_base, 'wb') as outfile:
while chunk := infile.read(chunk_size):
outfile.write(chunk)
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
@jthielen, out of curiosity what happens if you pass username and password directly to kwargs = dict(username=username, password=userpass)
remote_file = fsspec.open(filename, **kwargs) ? |
Beta Was this translation helpful? Give feedback.
-
You need to include your cookies in client_kwargs:
Note that fsspec's HTTP backend uses aiohttp, not requests. |
Beta Was this translation helpful? Give feedback.
You need to include your cookies in client_kwargs:
Note that fsspec's HTTP backend uses aiohttp, not requests.