简体   繁体   中英

Why I can't fill this form using Python “requests” library?

I want to fill a sign-in form using requests library in Python. As you see below, the name of fields are username and password : 在此处输入图片说明

So I wrote the below script:

session = requests.session()
p = session.post("http://192.168.94.1/SubSystem/Portal/Index1/Login/Login2.aspx", {'username':'myUsername','password':'myPassword'})
print 'headers', p.headers
print 'cookies', requests.utils.dict_from_cookiejar(session.cookies)
print 'html',  p.text

The problem is, when I run that, it returns the Sign-in page again! What's wrong with my program?

Update :

I also tried to send the parameters as data, but nothing changed. ie the below code returns the same output also :

payload = {'username': 'myUsername', 'password': 'myPassword'}
r = requests.post("http://192.168.94.1/SubSystem/Portal/Index1/Login/Login2.aspx", data=payload)
print(r.text)

Update2 :

I catch the packets using Burp Suit to see the difference :

This it the packet that my browser sends :

POST /SubSystem/Portal/Index1/Login/Login2.aspx HTTP/1.1
Host: 192.168.94.1
Proxy-Connection: keep-alive
Content-Length: 134
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: http://192.168.94.1
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://192.168.94.1/SubSystem/Portal/Index1/Login/Login2.aspx
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: ASP.NET_SessionId=nntwluu5uhrldzuymsv333uc; CURRENT_LANGUAGE_2=fa

__VIEWSTATE=%2FwEPDwUKMTY5ODAzNzY1MmRkpL4TXtJfODqn0yxypIJaOzkl4YUWynT8urN%2BYYXM%2FnY%3D&Command=LOGIN&username=myUsername&password=myPassword

And this it the packet that the second Python script sends :

POST /SubSystem/Portal/Index1/Login/Login2.aspx HTTP/1.1
Host: 192.168.94.1
Content-Length: 31
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: python-requests/2.4.3 CPython/2.7.0 Windows/7
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded

username=myUsername&password=myPassword

It looks like you firstly need to send a POST request, but from your recent update, that POST also expects some cookies available - there's also a hidden field called "Command" that needs to be sent with the value "LOGIN", so.

import requests

URL = "http://192.168.94.1/SubSystem/Portal/Index1/Login/Login2.aspx"

# Set up session object so we can store cookies across requests
session = requests.session()
# GET the page to get any initial cookies required for the POST
r = session.get(URL)
# POST to the login form (making sure to add "Command")
r = session.post(URL, data={'Command': 'LOGIN', 'username': 'x', 'password': 'y'})

As to why you don't get images when you save the page is that when a browser loads a page, it sees links to resources/stylesheets/images and issues further requests to access them - all requests does is load the "text" of the page as it were. There's ways of achieving this, but that's out of scope as an answer to the core question here.


As per the comment regarding Can I do multiple logins using these session objects? - here's one way of doing it...

# List of users and passwords
USERS = {
    'rod': 'password1',
    'jane': 'password2',
    'freddy': 'password3'
}
# Initial name->initial session with cookies
sessions = {name:requests.session().get(URL) for name in USERS}
# Login users
sessions.update(
    (name, session.post(URL, data={'Command': 'LOGIN', 'username': name, 'password': USERS[name]})) 
    for name, session in sessions.items()
)
# Now do something with users
r1 = sessions['rod'].get('http://something.com')
r2 = sessions['freddy'].get('http://something.com', params={'foo': 'bar'})
# etc...

Supporting @EsseTi's answer,Use data=Payload method to get them. .

import requests
payload = {'id_login': 'UserName', 'id_password': 'Password'}
url = #Your URL
something = requests.post(url, data=payload)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM