How to authenticate a site with Python using Requests? - python-3.x

How can I get this to work? I've been trying for days and keep hitting the same dead ends. I've tried all the examples I could find to no avail.
import requests
s = requests.Session()
payload = {'login_username': 'login', 'login_password': 'password'}
url = 'http://rustorka.com/forum/login.php'
requests.post(url, data=payload)
r2 = requests.get('http://rustorka.com/forum/index.php')
print(r2.text)
s.cookies

Related

python using requests and a webpage with a login issue

I'm trying to login to a website via python to print the info. So I don't have to keep logging into multiple accounts.
In the tutorial I followed, he just had a login and password, but this one has
Website Form Data
Does the _wp attributes change each login?
The code I use:
mffloginurl = ('https://myforexfunds.com/login-signup/')
mffsecureurl = ('https://myforexfunds.com/account-2')
payload = {
'log': '*****#gmail.com',
'pdw': '*****'
'''brandnestor_action':'login',
'_wpnonce': '9d1753c0b6',
'_wp_http_referer': '/login-signup/',
'_wpnonce': '9d1753c0b6',
'_wp_http_referer': '/login-signup/'''
}
r = requests.post(mffloginurl, data=payload)
print(r.text)
using the correct details of course, but it doesn't login.
I tried without the extra wordpress elements and also with them but it still just goes to the signin page.
python output
different site addresses, different login details
Yeah the nonce will change with every new visit to the page.
I would use request.session() so that it automatically stores session cookies and all that good stuff.
Do a session.GET('some_login_page.com')
Parse with the response content with BeautifulSoup to retrieve the nonce.
Then add that into the payload of your POST request when you login.
A very quick and dirty example:
import requests
from bs4 import BeautifulSoup as bs
email = 'test#email.com'
password = 'password1234'
url = 'https://myforexfunds.com/account-2/'
# Start a session
with requests.session() as session:
# Send a GET request to the login page
r = session.get(url)
# Check if the request was successful
if r.status_code != 200:
print("Get Request Failed")
# Parse the HTML content of the page
soup = bs(r.content, 'lxml')
# Extract the value of the nonce from the HTML
nonce = soup.find(id='woocommerce-login-nonce')['value']
# Set up the login form data
params ={
"username": email,
"password": password,
"woocommerce-login-nonce": nonce,
"_wp_http_referer": "/account-2/",
"login": "Log+in"
}
# Send a POST request with the login form data
r = session.post(url, params=params)
# Check if the request was successful
if r.status_code != 200:
print("Login Failed")

How do I pass an API Token into Cookie via Python Request?

I'm having issues w/ authenticating to pkgs.org api, a token was produced, by support mentioned it needs to be passed as a cookie. I've never worked with cookies before.
import requests
import json
import base64
import urllib3
import sys
import re
import os
token=('super-secret')
#s = requests.Session()
head = {'Accept':'application/json'}
r = requests.get('https://api.pkgs.org/v1/distributions', auth=(token), headers=head)
print(r)
print(r.connection)
print(r.cookies)
I tried to use the request.session method, to handle the cookie, but i honestly don't know syntax on how to ever 1 create a cookie, let alone pass the cookie.
If I read the API documentation correctly you should set acces_token cookie:
import requests
token = "super-secret"
cookies = {"access_token": token}
headers = {"Accept": "application/json"}
r = requests.get(
"https://api.pkgs.org/v1/distributions", cookies=cookies, headers=headers
)

Make multipart/form-data post requests with python

Im trying To Make post requests (multipart/form-data) in this website https://gofile.io/?t=api
every time time i got an error when i try to upload file
my code
import requests
req = requests.session()
files= {'file': open("test.txt", 'rb')}
response = req.post('https://srv-file7.gofile.io/upload', files=files)
print(response.text)
I got error every time ,are smething missing in the code
from requests_toolbelt.multipart.encoder import MultipartEncoder
import requests
import json
mp_encoder = MultipartEncoder(
fields={
'filesUploaded': ('file_name.txt', open('file_name.txt', 'rb'))
}
)
r = requests.post(
'https://srv-file9.gofile.io/upload',
data=mp_encoder,
headers={'Content-Type': mp_encoder.content_type}
)
scrap = r.json()
# send "Token file" 123Abc
Token = scrap['data']['code']
Check this. It will work.

Hanging delete request

Below returns all the info for the dashboards belonging to vhost 'test'
import requests
url = 'https://abcde/api/dashboards
headers = {'vhost' : 'test'}
r = requests.get(url, headers=headers, auth=('user' , '1234'))
print(r.text)
Now I would like to delete all the dashboards but what I have below hangs for minutes until I break out of it. The dashboards are still there viewed from the UI. I have no baseline as to how long a delete should take.
import requests
url = 'https://abcde/api/dashboards
headers = {'vhost' : 'test'}
r = requests.delete(url, headers=headers, auth=('test' , '1234'))
print(r.text)
Thank you
Using Postman, I received a 500 error
While the server was building the response a unexpected internal server error occured which forced the server to abort the request

Sending cookies with python request

I'm trying to make a post request with python's requests module.
I'm passing the headers, cookies, and data as part of the post request.
When I make the request,I get 400 bad request in the output.
I verified the payload and it looks correct to me. I'm wondering If my syntax for cookie is correct.
#!/usr/local/bin/python
import requests
session = "ASDFGHTR/=FGHYTYKSDMN="
lastmanaged = "9ycG9yYXRpb25fRGlyZWN0I"
header = {'akm-accept':'akm/cookie'}
cookie = {'SESSION': session,
'LASTMANAGED':lastmanaged}
data = {'client': '0ad3cfb66-4fa0-b94a-1bf712eae628&grant_type=password_assertion'}
url = "https://hostname-goes-here"
session = requests.Session()
response = session.post(url, headers=header, cookies=cookie, data=data,
verify=False, allow_redirects=False)
print(response.text)
print(response.status_code)
output:
{"code":"invalid_request","title":"One or more fields in request are invalid","incidentId":"f1fbba64-17e4-4d88-852f-01c137fa012e","requestId":"-5011561246754340090","solution":"none"}
400

Resources