Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #2553

Re: using python to post data to a form

References <4D9958FA.8080606@tysdomain.com>
Date 2011-04-03 23:24 -0700
Subject Re: using python to post data to a form
From Chris Rebert <clp2@rebertia.com>
Newsgroups comp.lang.python
Message-ID <mailman.184.1301898277.2990.python-list@python.org> (permalink)

Show all headers | View raw


On Sun, Apr 3, 2011 at 10:36 PM, Littlefield, Tyler <tyler@tysdomain.com> wrote:
> Hello:
> I have some data that needs to be fed through a html form to get validated
> and processed and the like. How can I use python to send data through that
> form, given a specific url? the form says it uses post, but I"m not really
> sure what the difference is.

They're different HTTP request methods:
http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods

The key upshot in this case is that GET requests place the parameters
in the URL itself, whereas POST requests place them in the body of the
request.

> would it just be:
> http://mysite.com/bla.php?foo=bar&bar=foo?

No, that would be using GET.

> If so, how do I do that with python?

Sending POST data can be done as follows (I'm changing bar=foo to
bar=qux for greater clarity):

from urllib import urlopen, urlencode

form_data = {'foo' : 'bar', 'bar' : 'qux'}
encoded_data = urlencode(form_data)
try:
    # 2nd argument to urlopen() is the POST data to send, if any
    f = urlopen('http://mysite.com/bla.php', encoded_data)
    result = f.read()
finally:
    f.close()

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

Cheers,
Chris
--
http://blog.rebertia.com

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: using python to post data to a form Chris Rebert <clp2@rebertia.com> - 2011-04-03 23:24 -0700

csiph-web