Othman Bargach
Othman Bargach

Reputation: 21

I struggle creating a script to connect a wifi with a captive portal

I'm trying to connect with a script to a wifi with a browser portal. I'm sending the http request with the post method and with a payload but the portal refuses the request as it is not sent threw my browser

Here is my code :

import requests

url = 'https://xxx/portal_api.php'

# Add appropriate credentials here
payload = {
    "action": "authenticate",
    "login": "[email protected]",
    "password": "xxxx",
    "policy_accept": "false",
    "from_ajax": "true"
}

# Send a POST request with the credentials
response = requests.post(url, data=payload)

payload2 = {
    "action": "switch_language",
    "isAppleCNA_fakeclick": "false",
    "lang": "fr"
}

# Check if the connection was successful
if response.status_code == 200:
    print("Connection successful 1")
    response2 = requests.post(url, data=payload2)
    if response2.status_code == 200:
        print("Connection successful 2")
else:
    print("Connection failed")

I expect to be connected and I get the two successfull messages but I'm not. When I try the requests on postman I get this response:

<html>

<head>
    <title>Forbidden access</title>
</head>

<body style="text-align:center">
    <div
        style="width:80%; border:1px solid #64696C; color:#64696C; font-weight:bold; text-align:center; padding:20px; margin:auto; margin-top:20px">
        <p id="small_p" style="font-size:small;">Forbidden access</p>
        <p id="large_p" style="font-size:large;">Your current web browser cannot access this page.</p>
    </div>
</body>

</html>

How can I make the portal believe my script is the wright browser to do this ?

Thanks !

Upvotes: 0

Views: 388

Answers (1)

antonin
antonin

Reputation: 56

It's been a while since you posted your message, but this could help others. Try doing this before any request:

AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0"
requests.utils.default_headers()["User-Agent"] = AGENT

The default user agent of the request module is currently python-requests/2.31.0, and some websites protect themselves from simple bots with such a quick browser-check.

Alternatively, you can specify the user agent each time you send a request:

requests.get("http://example.com", headers={"User-Agent": agent})

Hope this helps!

Antonin

Upvotes: 0

Related Questions