Python Forum
Python networking vs classes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python networking vs classes
#1
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
Reply
#2
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
Reply
#3
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.
Reply
#4
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
Reply
#5
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python classes and init method Alaweey98 9 7,097 Apr-03-2017, 12:02 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020