Created
November 9, 2016 08:10
-
-
Save tetafro/7e1eb8549c324835cf23a283d9e60aed to your computer and use it in GitHub Desktop.
Python requests example - auth cookie and download file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Naming file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.