Sending POST with Python through curl

0

I want to turn the form (which is working) into a curl to run on the backend

<form action="https://pt.stackoverflow.com/users/login?ssrc=head&returnurl=https%3a%2f%2fpt.stackoverflow.com%2f" method="post">

        <input type="text" name="email"></input>
        <input type="password" name="password"></input>

        <input type="submit" value="enviar"></input>
    </form>

Attempt to curl in python:

import urllib 
url = 'https://pt.stackoverflow.com/users/login?ssrc=head&returnurl=https%3a%2f%2fpt.stackoverflow.com%2f'
values = {'email': '[email protected]', 'password' : '$teste123','submit': 'SUBMIT'}
dados = urllib.urlencode(values)
html = urllib.urlopen(url,dados).read()
print (html)
    
asked by anonymous 28.02.2017 / 13:42

1 answer

1
  

"I want to turn the form into a curl to run on the backend"?!

You have some concepts to check out, and we are here to help you achieve this.

What you're doing is not using curl, it's a library ( urllib ) of python that in many aspects is similar and serves the same things.

What you are trying to do, login to stackoverflow, I doubt it will result, there should be security processes to prevent this, for example javascript loading of payloads to send to the server in the login process.

Assuming it would work, the correct way would be:

import urllib, urllib.parse, urllib.request
url = 'https://pt.stackoverflow.com/users/login?ssrc=head&returnurl=https%3a%2f%2fpt.stackoverflow.com%2f'
values = {'email': '[email protected]', 'password' : '$teste123','submit': 'SUBMIT'}
dados = urllib.parse.urlencode(values)
html = urllib.request.urlopen(url, dados.encode()).read().decode() # decode de bytes para string
print(html)

Testing a POST request:

import urllib, urllib.parse, urllib.request
url = 'http://httpbin.org/post'
values = {'email': '[email protected]', 'password' : '$teste123','submit': 'SUBMIT'}
dados = urllib.parse.urlencode(values)
html = urllib.request.urlopen(url, dados.encode()).read().decode() # decode de bytes para string
print(html)


See also:

Requests
Other ways to login

    
28.02.2017 / 14:32