Skip to content

Instantly share code, notes, and snippets.

@tetafro
Created November 9, 2016 08:10
Show Gist options
  • Save tetafro/7e1eb8549c324835cf23a283d9e60aed to your computer and use it in GitHub Desktop.
Save tetafro/7e1eb8549c324835cf23a283d9e60aed to your computer and use it in GitHub Desktop.
Python requests example - auth cookie and download file
import requests
BASE_URL = 'http://example.com'
AUTH_URL = BASE_URL + '/login'
CREDENTIALS = {'username': 'user', 'password': 'qwerty'}
session = requests.Session()
session.post(AUTH_URL, data=CREDENTIALS)
print(session.cookies)
file_url = BASE_URL + '/files/name.txt'
resp = session.get(file_url, stream=True)
if resp.status_code == 200:
filename ='/tmp/myfile.txt'
with open(filename, 'wb') as f:
for chunk in resp.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
@tetafro
Copy link
Author

tetafro commented Mar 15, 2023

This is a very basic case that I wrote here. Moodle might require Javascript to be executed for example. I'd suggest using something like Selenium for such cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment