0

There's a website I would like to login to on two different accounts and create two separate persistent HTTP connections. The only way I know how to do this is to have a separate login function twice but that would seem kind of redundant so I just made the function login(). But now I would like to somehow re-use the same login function to create multiple persistent HTTP sessions in my other functions but it goes out of scope.

def login(username, password):
    requests.post('example.com', data=payload)

def website1():
    login("matthew", "qwerty")
    session = requests.session()

def website2():
    login("matthew2", "qwerty2")
    session = requests.session()

1 Answer 1

2

You could use requests cookie jar which support multiple domains :

jar = requests.cookies.RequestsCookieJar()

def login(url, domain, username, password):
    r = requests.post(url, data=payload)
    jar.set('session', r.cookies['session'], domain=domain) # assuming its name is session

And then for using the session cookie on both sites :

requests.get(url, cookies=jar)

For more information :

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.