Skip to content

Instantly share code, notes, and snippets.

@enochchu
Created January 10, 2018 02:23
Show Gist options
  • Save enochchu/0afcb360fca27dfee22eb713f7931822 to your computer and use it in GitHub Desktop.
Save enochchu/0afcb360fca27dfee22eb713f7931822 to your computer and use it in GitHub Desktop.
Python Examples
import requests
## Login
client = requests.session()
self.login(client)
def login(self, client, url, username, password):
client.get(url)
cookies = requests.utils.dict_from_cookiejar(client.cookies)
crsftoken = cookies['csrftoken']
payload = {
'id_username': self.POOTLE_USER_NAME,
'id_password': self.POOTLE_PASSWORD,
'csrfmiddlewaretoken': crsftoken
}
result = client.post(url, data=payload, headers=dict(Referer=url))
return client
## Progress Bars (Download Example)
from clint.textui import progress
def download(self, client, url, folder, filename):
result = client.get(url, stream=True)
with open(folder + '/' + filename, 'wb+') as f:
total_length = int(result.headers.get('content-length'))
for chunk in progress.bar(result.iter_content(chunk_size=1024), expected_size=(total_length/1024) + 1):
if chunk:
f.write(chunk)
f.flush()
return client
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment