Tutorial :How do I send a HTTP POST value to a (PHP) page using Python?



Question:

I have a PHP page that has 1 textbox and when I press on the submit button. My SQL is going to store this product name into my database. My question is; is it possible to send/post the product name using Python script that asks for 1 value and then use my PHP page to send it to my database? Thanks!


Solution:1

Check out the urllib and urllib2 modules.

http://docs.python.org/library/urllib2.html

http://docs.python.org/library/urllib.html

Simply create a Request object with the needed data. Then read the response from your PHP service.

http://docs.python.org/library/urllib2.html#urllib2.Request


Solution:2

When testing, or automating websites using python, I enjoy using twill. Twill is a tool that automatically handles cookies and can read HTML forms and submit them.

For instance, if you had a form on a webpage you could conceivably use the following code:

from twill import commands  commands.go('http://example.com/create_product')  commands.formvalue('formname', 'product', 'new value')  commands.submit()  

This would load the form, fill in the value, and submit it.


Solution:3

I find http://www.voidspace.org.uk/python/articles/urllib2.shtml to be a good source of information about urllib2, which is probably the best tool for the job.

import urllib  import urllib2    url = 'http://www.someserver.com/cgi-bin/register.cgi'  user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'  values = {'name' : 'Michael Foord',            'location' : 'Northampton',            'language' : 'Python' }  headers = { 'User-Agent' : user_agent }    data = urllib.urlencode(values)  req = urllib2.Request(url, data, headers)  response = urllib2.urlopen(req)  the_page = response.read()  

The encoding is actually done using urllib, and this supports HTTP POST. There is also a way to use GET, where you have to pass the data into urlencode.

Don't forget to call read() though, otherwise the request won't be completed.


Solution:4

Yes. urllib2 is a nice Python way to form/send HTTP requests.


Solution:5

Personally i prefer to use Requests. As its summary, this is a HTTP library for Python, built for human beings.

To quickly solve problems, rather than study deeply in HTTP, Requests will be a better choice. (Yeah, i mean compared to urllib or socket)

For example, A python file which send a POST request with userdata:

import requests  userdata = {"firstname": "Smith", "lastname": "Ayasaki", "password": "123456"}  resp = requests.post('http://example.com/test.php', data = userdata)  

And the following text.php treating this request:

$firstname = htmlspecialchars($_POST["firstname"]);  $lastname = htmlspecialchars($_POST["lastname"]);  $password = htmlspecialchars($_POST["password"]);  echo "FIRSTNAME: $firstname  LASTNAME: $lastname  PW: $password";  

Finally, the response text (resp.content in python) will be like:

FIRSTNAME: Smith LASTNAME: Ayasaki PW: 123456


Solution:6

Just an addendum to @antileet's procedure, it works similarly if you're trying to do a HTTP POST request with a web-service-like payload, except you just omit the urlencode step; i.e.

import urllib, urllib2    payload = """  <?xml version='1.0'?>  <web_service_request>      <short_order>Spam</short_order>      <short_order>Eggs</short_order>  </web_service_request>  """.strip()  query_string_values = {'test': 1}  uri = 'http://example.com'    # You can still encode values in the query string, even though  # it's a POST request. Nice to have this when your payload is  # a chunk of text.  if query_string_values:      uri = ''.join([uri, '/?', urllib.urlencode(query_string_values)])  req = urllib2.Request(uri, data=payload)  assert req.get_method() == 'POST'  response = urllib2.urlopen(req)  print 'Response:', response.read()  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »