unable to Post data to a login form using urllib python v3.2.1
import urllib.parse
import urllib.request
import time
def __init__(self, parent= None):
QtGui.QWidget.__init__(self,parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
username = 'name'
password = 'pass'
while True:
try:
page=urllib.request.urlopen('http://10.100.56.55:8090/httpclient.html','&username='+username+'@da-iict.org&password='+password+'&btnSubmit=Login')
the_page=开发者_开发百科page.read()
I have been trying to get this piece of code to work. the alternative in python 2 works just fine but in python v3.2 I get the error " POST data should be bytes or an iterable number of bytes
import urllib.parse
import urllib.request
import time
username = 'name'
password = 'pass'
tm = 60
while True:
url = "http://10.100.56.55:8090/httpclient.html"
values = {'username': username, 'password': password, 'btnSubmit':'Login'}
data = urllib.parse.urlencode(values)
binary_data = data.encode('ascii')
req = urllib.request.Request(url, binary_data)
urllib.request.urlopen(req)
In Python3 they have changed it so that the data needs to be binary, thus you have to encode it first. ASCII is fine if your username/password does not include any non-ASCII characters.
精彩评论