登录并发送不符合请求的表单

问题描述 投票:1回答:1

我已经在hackthissite上编写了编程任务2的代码,只是一些乐趣和练习编码等。我已经完成了任务,但我无法让Python使用requests库登录并自动将我的代码上传到表单。

以下是我的代码中的代码段,代码无法正常工作,但仍然在页面内容中显示“您需要登录”:

def upload_code(key):
    login_url = 'https://www.hackthissite.org/user/login'
    btn_submit = 'Login'
    username = 'example_username'
    password = 'example_password'
    login = {
        'btn_submit': btn_submit,
        'password': password,
        'username': username,
    }

    session = requests.session()
    header_auth = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko',
        'referer': 'www.hackthissite.org/user/login'
    }
    auth = session.post(login_url, headers=header_auth, data=login)

    challange_url = 'https://www.hackthissite.org/missions/prog/2/index.php'
    header = {'referer': 'www.hackthissite.org/missions/prog/2/index.php'}
    data = {'solution': key}

    r = session.post(challange_url, headers=header, data=data, verify=True)
    print(r.content)

如果有人能告诉我我做错了什么,我会非常感激。

python authentication login python-requests
1个回答
1
投票

你必须设置cookie。

我已经尝试过很多方法从网站上获取cookie,但似乎它提供了无效的cookie。

但是,您可以在浏览器中打开网络(如果您使用的是Chrome,请按Cntrl + E,如果您使用的是Firefox,请按Cntrl + Shift + E),然后访问login page以查找请求的有效PHPSESSID Cookie登录Cookies。

然后您可以在下面的代码中粘贴PHPSESSID cookie值:

from requests import Session

login_url = "https://www.hackthissite.org/user/login"
page_url = "https://www.hackthissite.org/missions/prog/2/index.php"

PHPSESSID = f"PHPSESSID=2r4t6qg89b8k89b871q4b6q5i3" # Cookie example. Enter yours.

login_headers = {
    "Host": "www.hackthissite.org",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "en-US,en;q=0.5",
    "Accept-Encoding": "gzip, deflate, br",
    "Referer": "https://www.hackthissite.org/missions/prog/2/index.php",
    "Content-Type": "application/x-www-form-urlencoded",
    "Cookie": PHPSESSID,
    "Content-Length": "62",
    "Connection": "keep-alive",
    "Upgrade-Insecure-Requests": "1",
    "DNT": "1",
}

login_data = {
    "username": "Your username",
    "password": "Your password",
    "btn_submit": "Login",
}



page_headers = {
    "Host": "www.hackthissite.org",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "en-US,en;q=0.5",
    "Accept-Encoding": "gzip, deflate, br",
    "Referer": "https://www.hackthissite.org/missions/prog/2/index.php",
    "Connection": "keep-alive",
    "Cookie": "PHPSESSID=2r4t6qg8eb8j89b871q4b6q0i3",
    "Upgrade-Insecure-Requests": "1",
    "DNT": "1",
}

page_data = {
    "solution": "Dontevenknow",
}

with Session() as session:
    session.post(login_url, headers=login_headers, data=login_data)
    response = session.post(page_url, headers=page_headers, data=page_data)

    print("Authenticated:", "Login Required" not in response.text)

它适用于我,我测试了Firefox和Chrome。

© www.soinside.com 2019 - 2024. All rights reserved.