How do I verify myself when using Python request posts - python

I can login and get a website, and post some messages. But on the other hand, some of the post calls failed, and the website return a message, which ask me to login again.
I think the reason is that I do not verify myself while posting, and the server find it and break this connection. But how to? I'm using mac and python 2.7.
I use this code to login:
Connection = requests.session()
result = Connection.post(url, headers = headd, data = data)
and success
This is other post codes after I login:
result = Connection.post(url, headers = headers, data = data)
but failed.
I also tried this:
result = Connection.post(url, headers = headers, data = data, verify=False)
But failed again. The url here is an https website. Does it matter? I mean, how to verify myself if necessary. Because I think it's the website who reject the post and break the session.

try using:
s = requests.session()
s.post(url, headers = headers, data = data)
instead of
Connection.post(...)

Related

POST request fails "The CSRF token could not be verified" (Python 3)

I have been trying to log on to this site with the python requests module, but I keep seeing "The CSRF token could not be verified". I have tried doing what other answers said but it doesn't seem to work.
client = requests.Session()
url = 'https://www.biopharmcatalyst.com/account/login/'
client.get(url)
token = client.cookies['CRAFT_CSRF_TOKEN']
headers = {'Cookie':token}
print(token)
login_data = {'loginName':'login',
'password':'pass',
'CRAFT_CSRF_TOKEN':token}
r1=client.post(url,data=login_data, headers=dict(Referer=url))
print(r1.text)
I'm really not sure what I'm doing wrong here. When I go to the html, I see a different value for the CRAFT_CSRF_TOKEN than what the cookie shows under headers.
The page is setting the CRSF token using JS and the DOM.
You will need to parse the initial GET request to get the token and then pass that to your login.
Something like the following:
import re
client = requests.Session()
url = 'https://www.biopharmcatalyst.com/account/login/'
r = client.get(url)
match = re.search(r'window.csrfTokenValue = "(.*?)";', r.text)
if match:
crsf = match.group(1)
login_data = {
'loginName':'login',
'password':'pass',
'CRAFT_CSRF_TOKEN':crsf}
Handling the headers and so forth will be different depending on what happens after you get the login (I don't have an account).

I want to send a Python request to an ASP site but the site show access denied

Site url is http://rajresults.nic.in/resbserx18.htm when send data, but when response comes URL changes in ASP. So which URL user need to send request ASP or html?
Request:
import requests
# data for get result
>>> para = {'roll_no':'2000000','B1':'Submit'}
# this is url where data is entered and get asp response
>>> url = 'http://rajresults.nic.in/resbserx18.htm'
>>> result = requests.post(url,data=para)
>>> result.text
Response
'The page you are looking for cannot be displayed because an invalid method (HTTP verb) is being used.'
Okay after a little bit of work, I found it's some issue with the headers.
I did some trial and error, and found that it checks to make sure the Host header is set.
To debug this, I just incrementally removed chrome's request headers and found which one this web service was particular about.
import requests
headers = {
"Host": "rajresults.nic.in"
}
r = requests.post('http://rajresults.nic.in/resbserx18.asp',
headers = headers,
data = {'roll_no': 2000000, 'B1': 'Submit'}
)
print(r.text)

Unable to do HTTP post in Python

I'm trying to configure an Access Point(AP) in my office through HTTP Post method via Python.
I was able to login to the AP through Python HTTP Authentication code but when I click on wireless page of the AP to give inputs such as AP SSID, Channel and Passphrase, I'm getting stuck at this point. There is a apply button at the end of the wireless page.
When I'm trying to do that using the below mentioned code, I don't see any changes getting reflected at the AP side. May be my code is wrong or I'm not following the correct procedure to post the data in the AP. How can I resolve this issue?
import urllib2
import requests
def login():
link = "http://192.168.1.11/start_apply2.htm"
username = 'admin'
password = 'admin'
p = urllib2.HTTPPasswordMgrWithDefaultRealm()
p.add_password(None, link, username, password)
handler = urllib2.HTTPBasicAuthHandler(p)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
age = urllib2.urlopen(link).read()
payload = {'wl_ssid_org': 'nick','wl_wpa_psk_org':12345678}
r = requests.get(link)
r = requests.get(link, params=payload)
r = requests.post(link, params=payload)
login()
Note: When I'm running this code, it was throwing error as: 401 unauthorized. When I'm able to login to the AP using same auth code but why I'm unable to clear the authentication here, I'm not getting it.
In your case, you should change
r = requests.post(link, params=payload)
to
r = requests.post(link, data=payload)
Then you can do the POST request successfully.
You should refer to the requests document to find more tutorials.
Even, you can replace the code using urllib2 with code using requests.

Check if post request logged me in

I am trying to log in with a post request using the python requests module on a MediaWiki page:
import requests
s = requests.Session()
s.auth = ('....', '....')
url = '.....'
values = {'wpName' : '....',
'wpPassword' : '.....'}
req = s.post(url, values)
print(req.content)
I can't tell from the return value of the post request whether the login attempt was succesful. Is there something I can do to check this? Thanks.
Under normal circumstances i would advise you to go the mechanize way and make things way too easy for yourself but since you insist on requests, then let us use that.
YOu obviously have got the values right but i personally don't use the auth() function. So, try this instead.
import requests
url = 'https://example.com/wiki/index.php?title=Special:UserLogin'
values = {
'wpName': 'myc00lusername',
'wpPassword': 'Myl33tPassw0rd12'
}
session = requests.session()
r = session.post(url, data=values)
print r.cookies
This is what I used to solve this.
After getting a successful login, I read the texts from
response.text
and compared it to the text I got when submitting incorrect information.
The reason I did this is that validation is done on the server side and Requests will get a 200 OK response whether it was successful or not.
So I ended up adding this line.
logged_in = True if("Incorrect Email or password" in session.text) else False
Typically such an authentication mechanism is implemented using HTTP cookies. You might be able to check for the existence of a session cookie after you've authenticated successfully. You find the cookie in the HTTP response header or the sessions cookie attribute s.cookies.

Python use request to login

my target is to login within this website:
http://www.six-swiss-exchange.com/indices/data_centre/login.html
And once logged, access the page:
http://www.six-swiss-exchange.com/downloads/indexdata/composition/close_smic.csv
To do this, I am using requests (password and email are unfortunately fake there):
import requests
login_url = "http://www.six-swiss-exchange.com/indices/data_centre/login_en.html"
dl_url = "http://www.six-swiss-exchange.com/downloads/indexdata/composition/close_smic.csv"
with requests.Session() as s:
payload = {
'username':'GG#gmail.com',
'password':'SummerTwelve'
}
r1 = s.post(login_url, data=payload)
r2 = s.get(dl_url, cookies=r1.cookies)
print 'You are not allowed' in r2.content
And the script always returns False. I am using Chrome and inspect to check the form to fill, this is the result of inspect when I manually login:
payload = {
'viewFrom':'viewLOGIN',
'cugname':'swxindex',
'forward':'/indices/data_centre/adjustments_en.html',
'referer':'/ssecom//indices/data_centre/login.html',
'hashPassword':'xxxxxxx',
'username':'GG#gmail.com',
'password':'',
'actionSUBMIT_LOGIN':'Submit'
}
I tried with this, with no result, where XXXXX is the encoded value of SummerTwelve... I clearly do not know how to solve this out! Maybe by mentionning the headers ? The server could reject script request?
I had a similar problem today, and in my case the problem was starting the website interaction with a 'post' command. Due to this, I did not have a valid session cookie which I could provide to the website, and therefore I got the error message "your browser does not support cookies".
The solution was to load the login-page once using get, then send the login-data using post:
s = requests.Session()
r = s.get(url_login)
r = s.post(url_login, data=logindata)
My logindata corresponds to your payload.
With this, the session cookie is managed by the session and you don't have to care about it.

Categories