Member Avatar for yoni0505

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.

Member Avatar for yoni0505

You got me wrong, its not about the decoding, its about sending a form.
it seems that the doesn't send the "user" and "pass".
my question is how do I fix this problem?

I found the solution, I had to send it to "http://yoni-examples.freehostia.com/scam/form.php"

You got me wrong, its not about the decoding, its about sending a form.
it seems that the doesn't send the "user" and "pass".
my question is how do I fix this problem?

I found the solution, I had to send it to "http://yoni-examples.freehostia.com/scam/form.php"

How could it have worked with the wrong encoding?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.