http POSTing

Duncan Booth duncan at rcp.co.uk
Wed Jan 5 09:31:10 EST 2000


newshoes at mail.com (NewShoes) wrote in 
<8EB19A9E7newshoesmailcom at 205.147.63.94>:

>i'm trying to create an offline form i can send to a website
>through "POST". being new to python and minimal knowledge of
>the http protocol, this has stumped me rather quick.
>
>obviously the urllib is out, since it seems i can only
>perform "GET" requests. but i can't figure out if the
>httplib is lowlevel enough, or if i need to go down to sockets
>
>anyways, if someone has an HTML form like this...
>
>
><FORM ACTION="http://www.test.com/form.html" METHOD="POST">
><INPUT TYPE="hidden" NAME="hidd" VAL="en">
><INPUT TYPE="text" NAME="email" VALUE="you at yourmail.com">
><INPUT TYPE="image" SRC=post.gif NAME="POST">
></FORM>
>
>
>how would one go about sending this off if filled out
>from a simple TKinter form??
>(quick example code being what i seek here) :]
>

What is your objection to urllib? It does POST requests just fine.
Try the following:

import string,urllib
def format(items):
    list = []
    for k, v in items:
       list.append("%s=%s" % (k, urllib.quote_plus(v)))
    return string.join(list, '&')

data = { 'hidd':'en', 'email':'you at yourmail.com',
         'POST':'post.gif' }
d = format(data.items())
try:
    sock = urlopen(YourURL, d)
    for l in sock.readlines():
        print l
    sock.close()
    finally:
        urlcleanup()



More information about the Python-list mailing list