Sending POST with Python via Curl

Asked

Viewed 979 times

1

I want to turn the form (which is working) into a URL to run in the backend

<form action="/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>

Python Curl try:

import urllib 
url = '/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)

1 answer

1


"I want to turn the form into a URL to run in backend"?!

You have some concepts to work out, and we’re here to help you get that.

What you’re doing is not using Curl, it’s using a library (urllib) python that in many ways is similar and serves the same things.

What you’re trying to do, log in to stackoverflow, I doubt it will work that way, there should be security processes to prevent that, for example, javascript loading payloads to send to the server in the login process.

Assuming it would work, the right way would be:

import urllib, urllib.parse, urllib.request
url = '/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

  • Thanks! Which of the 2 ways is better? #modo1 r = requests.post(url, data=value) print(r. text) "" #Modo2 data = urllib.parse.urlencode(value) html = urllib.request.urlopen(url, data.Encode(). read(). Decode() print (html)

  • @Daniloalbergardi personally prefer to use requests for knowing better and for me to be simpler. But you achieve the same with urlib, it’s a matter of taste

  • @Daniloalbergardi on your question: http://stackoverflow.com/questions/2018026/what-are-the-differences-between-the-urllib2-and-requests-module

  • OK, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.