Skip to content

Instantly share code, notes, and snippets.

@guoguo12
Created January 25, 2014 08:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guoguo12/8613515 to your computer and use it in GitHub Desktop.
Save guoguo12/8613515 to your computer and use it in GitHub Desktop.
Demonstrates how to use Requests (http://docs.python-requests.org/en/latest/) to login to Edline (http://www.edline.net/).
import requests
HEADERS = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36'}
LOGIN_URL = 'https://www.edline.net/InterstitialLogin.page'
LOGIN_POST_URL = 'https://www.edline.net/post/InterstitialLogin.page'
class Edline(object):
def __init__(self, username, password, login=True):
self.username = username
self.password = password
if login:
self.login()
self.get_home()
def get_login_cookies(self):
rsp = requests.get(LOGIN_URL, headers=HEADERS)
if rsp.status_code != 200:
raise IOError('Failed to get login page. Status code: %d.' % rsp.status_code)
return rsp.cookies
def login(self):
payload = {'TCNK': 'authenticationEntryComponent',
'submitEvent': '1',
'guestLoginEvent': '',
'enterClicked': 'true',
'bscf': '',
'bscv': '',
'targetEntid': '',
'ajaxSupported': 'yes',
'screenName': self.username,
'kclq': self.password}
cookies = self.get_login_cookies() # Get cookies needed to log in
rsp = requests.post(LOGIN_POST_URL, headers=HEADERS, data=payload, cookies=cookies, allow_redirects=False)
if rsp.status_code != 302:
raise IOError('Login POST request failed. Status code: %d.' % rsp.status_code)
if 'Location' not in rsp.headers:
raise ValueError('Login POST request response contained no redirect URL.')
if rsp.headers['Location'].endswith('Notification.page'):
raise IOError('Login failed, possibly due to incorrect login credentials.')
cookies.update(rsp.cookies) # Add new session cookies to cookie jar
self.home_url = rsp.headers['Location']
self.cookies = cookies
def get_home(self):
rsp = requests.get(self.home_url, headers=HEADERS, cookies=self.cookies)
return rsp
def write_content(rsp, filename='file.html'):
f = open(filename, 'w')
f.write(rsp.content)
f.close()
if __name__ == '__main__':
e = Edline('USERNAME', 'PASSWORD')
write_content(e.get_home())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment