简体   繁体   中英

How can I convert this Curl call into Python 3?

I have the following curl that successfully logs me into a website. It returns a 302 and a cookie:

curl \
--verbose \
--request POST \
--data '__EVENTTARGET=&body%3Ax%3AtxtLogin=kingkong%40mailinator.com&body%3Ax%3AbtnLogin=&crypted_pass=736015615f9e251692a6a4aa8a7baa14' \
"https://emma.maryland.gov/page.aspx/en/usr/login"

Unfortunately, I have to insert the real username & encrypted password of the account. otherwise this curl it won't work. However, this is a completely dummy account with NO private data in it. So please don't get mad at me.

I want to convert it to a Python3 code using requests.post() . So I made this code:

>>> requests.post(
...     url='https://emma.maryland.gov/page.aspx/en/usr/login',
...     data={
...         '__EVENTTARGET': '',
...         'body:x:txtLogin': 'kingkong@mailinator.com',
...         'body:x:btnLogin': '',
...         'crypted_pass': '736015615f9e251692a6a4aa8a7baa14'
...     }
... )
<Response [200]>

But the response I get from the Python3 code (200) doesn't match the response I get from the Curl (302). This means that the target server senses a difference between the two requests.

How can I convert the curl to Python3 that sends the exact same underlying HTTP request?

Your requests code is actually smarter than the cURL command.

HTTP 302 - is a redirect, cURL didn't follow it and gave you the first response it got. You can make cURL follow the redirect with -L : Is there a way to follow redirects with command line cURL?

The requests code followed the redirect and gave you the final response, which happened to be a HTTP 200.

Try your curl command with -L and see if you get HTTP 200 or not.

Alternatively, you can ask requests to not follow redirects with the allow_redirects=False option: Is there an easy way to request a URL in python and NOT follow redirects?

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