Hey.I try to write a class that has url, login and password, and also cookies and content and I dont know how to approach this task, doing it without classes is easy but I have to learn this too.
class loginTest():
def __init__(self):
s = requests.Session()
def login(self,login_user,login_pass):
data = {'username': login_user,'password': login_pass}
return self.s.post('targetURL', data=data)
def showMe(self,cookies,content):
request = self.s.get("targetURL")
content = self.s.content
cookies = self.s.cookies.get_dict()
print(content,cookies)
a = loginTest()
a.login
a.showMe
You don't call method(a.login
()
) and it has to be
self.s
in __init__.
Can get away with
a.login
if use
@property
.
cookies,content
you get in
show_me
method so nothing shall in.
import requests
class LoginTest():
def __init__(self):
self.s = requests.Session()
@property
def login(self,login_user, login_pass):
params = {'username': login_user, 'password': login_pass}
return self.s.post('target_url', params=params)
@property
def show_my(self):
request = self.s.get("targt_url_after_login")
content = self.s.content
cookies = self.s.cookies.get_dict()
print(content, cookies)
a = LoginTest()
a.login
a.show_me
Thanks for replying!
I think I understand more now, but still have some issues with making it work:
Quote:a.login
TypeError: login() missing 2 required positional arguments: 'login_user' and 'login_pass'
The same with giving the params in parentheses:
Quote:a.login(login_user,login_pass)
TypeError: login() missing 2 required positional arguments: 'login_user' and 'login_pass'
Looks like it doesnt see my variables with credentials somehow.
It should only be @property in last method,my mistake.
import requests
class LoginTest():
def __init__(self):
self.s = requests.Session()
def login(self,login_user, login_pass):
params = {'username': login_user, 'password': login_pass}
return self.s.post('target_url', params=params)
@property
def show_my(self):
request = self.s.get("targt_url_after_login")
content = self.s.content
cookies = self.s.cookies.get_dict()
print(content, cookies)
a = LoginTest()
a.login('kent', '1234')
a.show_me
Oh, also had to edit requests.Session cause it has no content attribute but it works like a charm now, thanks man, you are amazing!
Do you know some good sources to learn more practical coding with classes like this? I did finished some courses but it was functional mostly or just clearly theoretical oop.
Cheers!