Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
    
    
    
@S.Lott: Sending a file is different, because it's typically form/multipart data. I'd say this question is legit. Perhaps too much so to quit. –  cdleary Mar 11 '09 at 11:27
add comment

5 Answers 5

up vote 5 down vote accepted

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

share|improve this answer
add comment

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.

share|improve this answer
    
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 Mar 11 '09 at 4:15
    
"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 Mar 11 '09 at 6:51
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

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()
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.