Home > Enterprise >  Python Requests - Can't use a string in my request
Python Requests - Can't use a string in my request

Time:10-25

I am trying to login to a website using python requests. I want to use a string input to give the user the option to use their own email and password. But when I try to use the string I get the following error:

  File "c:\Users\NAME\Desktop\newcode\checkout.py", line 93, in <module>
    Checkout()
  File "c:\Users\NAME\Desktop\newcode\checkout.py", line 12, in Checkout
    'inUserName': LOGIN,
NameError: name 'LOGIN' is not defined

Here is my code:

from profiles import profiles




def Checkout():

    profiles()

    login = {
        'inUserName': LOGIN, 
        'inUserPass': PASSWORD

    }

    with requests.Session() as s:
        p = s.post('https://EXAMPLEWEBSITE.com/account/login', data=login)

Here is the file profiles :

def profiles():
    LOGIN = input("Enter your login email: ")
    print(LOGIN)
    PASSWORD = input("Enter your login password: ")
    print(PASSWORD)

CodePudding user response:

LOGIN and PASSWORD are stored in the wrong scope. They are not available within your dictionary. You can return LOGIN and PASSWORD from your profiles method to store them in the right scope.

def profiles():
    LOGIN = input("Enter your login email: ")
    print(LOGIN)
    PASSWORD = input("Enter your login password: ")
    print(PASSWORD)
    return LOGIN, PASSWORD

and then use it like this:

LOGIN, PASSWORD = profiles()
login = {
    'inUserName': LOGIN, 
    'inUserPass': PASSWORD
}

Now LOGIN and PASSWORD are available within your dictionary.

CodePudding user response:

Code:

def Checkout():

    LOGIN, PASSWORD = profiles()
    login = {
        'inUserName': LOGIN, 
        'inUserPass': PASSWORD

    }

    with requests.Session() as s:
        p = s.post('https://EXAMPLEWEBSITE.com/account/login', data=login)
        
def profiles():
    LOGIN = input("Enter your login email: ")
    print(LOGIN)
    PASSWORD = input("Enter your login password: ")
    print(PASSWORD)
    return LOGIN, PASSWORD
  • Related