tags:

views:

1979

answers:

6

Hi all! 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!

+5  A: 

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

kkubasik
+2  A: 

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

Hank Gay
+2  A: 

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.

Jerub
While twill is really convenient for testing scripts, it is somewhat broken in 2 ways.1) Must parse HTML before it could post.2) Cannot always handle invalid HTML without crashing.
kkubasik
"Doesn't work with forms that don't exist or are invalid" are two things that I don't care about. This is not a 'broken' thing.
Jerub
+2  A: 

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.

antileet
A: 

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()
cdleary
A: 

sorry. I noticed this is a place for answers not for questions. I have removed my question from here in input it to questions.