Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use 'pass' as a dictionary keyword

I'm trying to make a fairly basic requests script which signs into a website, however I'm running into a problem; the website requires the 'pass' key, however whenever I try to use this python throws an error saying invalid syntax. Here's my code:

import requests

with requests.Session() as c:
    url = 'www.a_url.com'
    USERNAME = 'a username'
    PASSWORD = 'a password'
    c.get(url)
    login_data = dict(user=USERNAME, pass=PASSWORD, jsok='1', dologin='Login')
    c.post(url, data=login_data, headers={"Referer": 'www.a_referer.com'})
    page = c.get('www.a_page.com')
    print(page.content)
    output = open('test.html','w')
    output.write(str(page.content))

And the error I'm given is:

    login_data = dict(user=USERNAME, pass=PASSWORD, jsok='1', dologin='Login')
                                        ^
SyntaxError: invalid syntax

Any idea how I can force python to ignore the 'pass' and just use it as the dict key?

like image 843
KlusterMonkey Avatar asked Aug 02 '26 06:08

KlusterMonkey


1 Answers

Since pass is a keyword, it can't be used as an identifier. You'll need to use a dict-literal for this:

login_data = {
    'user': USERNAME,
    'pass': PASSWORD,
    'jsok': '1',
    'dologin': 'Login',
}
like image 112
mgilson Avatar answered Aug 03 '26 19:08

mgilson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!