Skip to content

Instantly share code, notes, and snippets.

@SakuraSa
Created September 23, 2014 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SakuraSa/bbfd422c56b69991cf2c to your computer and use it in GitHub Desktop.
Save SakuraSa/bbfd422c56b69991cf2c to your computer and use it in GitHub Desktop.
Github_login.py
#!/usr/bin/env python
#coding=utf-8
import requests
from BeautifulSoup import BeautifulSoup
username = 'MY GITHUB ACCOUNT'
password = 'MY GITHUB PASSWORD'
session = requests.Session()
#get authenticity_token
page_login = BeautifulSoup(session.get('https://github.com/login').text)
post_form = page_login.find('div', id='login')
csrf_token = page_login.find('head').find('meta', attrs={'name':'csrf-token'})['content']
headers = {'X-CSRF-Token': csrf_token}
payload = {
ip['name']: ip['value']
for ip in post_form.findAll('input')
if ip.get('value', None)
}
payload['login'] = username
payload['password'] = password
#send post
rsp = session.post('https://github.com/session', data=payload, headers=headers)
if session.cookies['logged_in'] != 'yes':
print 'login failed.'
else:
print 'login success.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment