0

I tried to log in into a website using post requests in python by passing the username and password using the below code and it worked.

import requests
with requests.Session() as c:
    url='http://testing-ground.scraping.pro/login?mode=login'
    usr='admin'
    pwd='12345'
    c.get(url)
    print(c.cookies)
    login_data=dict(usr=usr,pwd=pwd)
    res=c.post(url,data=login_data)
    print(res)
    page=c.get('http://testing-ground.scraping.pro/login?mode=welcome')
    print(page.content)

Now,I want to try the same(log in to the same website using post requests) in lambda aws in python.I am a beginner to lambda,aws and have no idea how to proceed in lambda.

1 Answer 1

1

You have to modify the code to run in lambda environment.

For that you will need a lambda handler function as described here:

Also requests is not provided anymore in lambda. Thus, the easiest way to add requests to your lambda function would be through lambda layers as described in section Using AWS Lambda Layers of:

The crude version of your code for lambda would be (assuming requests layer is setup):

import requests

def handler(event, context):

    with requests.Session() as c:
        url='http://testing-ground.scraping.pro/login?mode=login'
        usr='admin'
        pwd='12345'
        c.get(url)
        print(c.cookies)
        login_data=dict(usr=usr,pwd=pwd)
        res=c.post(url,data=login_data)
        print(res)
        page=c.get('http://testing-ground.scraping.pro/login?mode=welcome')
        print(page.content)
Sign up to request clarification or add additional context in comments.

2 Comments

Should I use any json.loads () while modifying the code?I just read some of the articles and they used json.loads and also urllib3.Thats why I am a bit confused.
@lakshmi I added an example code. If you don't want to use requests layer than you can use python's build in tools for http functionality.

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.