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!
-
Duplicate: http://stackoverflow.com/questions/111945/is-there-anyway-to-do-http-put-in-python, http://stackoverflow.com/questions/393738/programmatic-form-submit, http://stackoverflow.com/questions/571083/in-python-how-might-one-log-in-answer-a-web-form-via-http-post-not-url-encoded – S.Lott Mar 10 '09 at 10:07
-
Duplicate: http://stackoverflow.com/questions/68477/send-file-using-post-from-a-python-script, http://stackoverflow.com/questions/150517/send-file-using-post-from-a-python-script – S.Lott Mar 10 '09 at 10:07
-
1@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
6 Answers
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.

- 3,684
- 6
- 25
- 23
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.

- 41,746
- 15
- 73
- 90
-
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 04: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 06:51
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.

- 1,577
- 1
- 11
- 15
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
- A simple Quickstart: http://docs.python-requests.org/en/latest/user/quickstart/
- Another answer recommending Requests: Sending data using POST in Python to PHP

- 11
- 4
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()