I'm trying to learn how to send forms using python 3.1 and it doesn't go well.

I've made a simple site with a POST form. it got user and pass fields, and it will save the data into a text file. (for debugging)
The site is working, I can type in anything, press submit and it will be saved.

I want to do this action with python, send the form.
I've made this code and it seems to don't work:

import urllib.parse
import urllib.request

url = 'http://yoni-examples.freehostia.com/scam/' #site I've made to test
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values = {'user' : 'blabla',
          'pass' : '1337'}
headers = { 'User-Agent' : user_agent }

data = urllib.parse.urlencode(values)
req = urllib.request.Request(url, data, headers)
response = urllib.request.urlopen(req)
the_page = response.read()
text = the_page.decode("utf8")
print (text) #show the page source, to see I have a response

input('Press any key to continue...') #pause

So I'm asking for help, can you help me fix the code?
It seems I'm missing something :\

You can print out the_page as a byte string, that seems to work.
However the_page.decode("utf8") gives a UnicodeEncodeError. It seems that utf8 is the incorrect character set. Use the_page.decode("iso-8859-1"), that character set works.

Edited 6 Years Ago by vegaseat: n/a

This article has been dead for over six months. Start a new discussion instead.