-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogin_leetcode.py
More file actions
28 lines (23 loc) · 1.03 KB
/
login_leetcode.py
File metadata and controls
28 lines (23 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import requests,json
from requests_toolbelt import MultipartEncoder
session = requests.Session()
user_agent = r'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'
def login(username, password):
url = 'https://leetcode.com'
cookies = session.get(url).cookies
for cookie in cookies:
if cookie.name == 'csrftoken':
csrftoken = cookie.value
url = "https://leetcode.com/accounts/login"
params_data = {
'csrfmiddlewaretoken': csrftoken,
'login': username,
'password':password,
'next': 'problems'
}
headers = {'User-Agent': user_agent, 'Connection': 'keep-alive', 'Referer': 'https://leetcode.com/accounts/login/', "origin": "https://leetcode.com"}
m = MultipartEncoder(params_data)
headers['Content-Type'] = m.content_type
session.post(url, headers = headers, data = m, timeout = 10, allow_redirects = False)
is_login = session.cookies.get('LEETCODE_SESSION') != None
return is_login