What does this python error mean?

Using python 3 in 2008R2:   I had some help getting this far but I do not understand the error at the bottom. Can someone help?

#We need urllib module
import urllib.parse
import urllib.request
#These are the user and password parameters, check that field names are the same at the page form and put your real user and password values on myuser and mypassword
params = urllib.parse.urlencode({'username': 'UID', 'password': 'password', 'submit' : 'login'})
#This is the url of the login page, put the real url here
site = "https://mysite.mycompany.com"
#We save the result of the post in variable f
f = urllib.request.urlopen(site, params)
#We read the result
result = f.read()
#We print the result, so we can see if we are logged in
#print result
result = open('c:\\phython.html',w)
#Finished, so we close f

Still getting error:

TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Walter RitzelSenior Software EngineerCommented:
According to the example below, you code is missing one encode:
data = urllib.parse.urlencode(values)
binary_data = data.encode(encoding) #encoding here could be utf-8, ascii, etc... 
req = urllib.request.Request(url, binary_data)

Open in new window

This is required and it is on Python manuals:
data should be a buffer in the standard application/x-www-form-urlencoded format. The urllib.parse.urlencode() function takes a mapping or sequence of 2-tuples and returns a string in this format. It should be encoded to bytes before being used as the data parameter.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.