KlusterMonkey
KlusterMonkey

Reputation: 85

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?

Upvotes: 2

Views: 85

Answers (2)

mgilson
mgilson

Reputation: 309929

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',
}

Upvotes: 10

Mayli
Mayli

Reputation: 571

Short answer, you cannot do so in that syntax. pass is a reserved keyword for python, such as return.

The alternative way of setting arbitrary object as key is to use this syntax:

login_data = {"user": USERNAME, "pass": PASSWORD, ...}

This is more generic and you can use anything as key.

{None: 1, 1: 2, str: int}

Upvotes: 0

Related Questions