Submit POST form with python

2

I'm developing an API in my work, and I need to develop something in python2 that sends a form to the server through POST, I saw some questions here in the forum and I could not find anything that worked ... I need the community to help me !

Well, I'm using this code to understand logic and move on to the senior programmer of the project:

import json
import urllib2

data = {
    'id' : 1, 'number': 556291406183, 'message' : 'Hello'
}

req = urllib2.Request('http://lsweb.net.br/gabriel.php')
req.add_header('Content-Type', 'application/json')

response = urllib2.urlopen(req, json.dumps(data))

But I'm not getting results with this code, so it does not send anything to the server. The intention was to send to the server page in POST and POST to read in PHP. HELP ME

    
asked by anonymous 16.12.2017 / 01:49

1 answer

1
#Instale a biblioteca requests pip3 install requests urllib3 --upgrade

import requests
payload = {'username': 'meuusername123', 'password': 'senha1234'} #1

resposta = requests.post("http://httpbin.org/formulario-post", data=payload) #2

# 1 After detecting the fields of the HTML code that need to be filled, let's say it was a login form with username and password, the data is grouped to use them in the POST request

# 2 The post method of the requests module is used to send form data. Where you should know through a basic analysis of the code which url send the POST method.

A real example with the website pastebin.com (Sending login data)

URL where the form is contained: link TAG values contained in the form:

Campo de usuário: user_name
Campo de senha: user_password
URL para envio do formulário: /login.php # onde completaremos do o domínio pastebin.com

It would look like this:

import requests
payload = {'user_name': 'meulogin', 'user_password': 'minhasenha123'}
resposta = requests.post("https://pastebin.com/login.php", data=payload)
#print(resposta.text)

Gross HTML

<form class="login_form" id="myform" method="post" action="/login.php">
...
</form>


<input type="text" name="user_name" maxlength="20" size="20" value="" 
...
</input>

<input type="password" name="user_password" size="20" value="" placeholder="Your 
...
</input>
    
17.12.2017 / 14:51