Seite 1 von 1

urlencode für POST "data should be bytes"

Verfasst: Montag 3. Oktober 2011, 16:14
von chefhose
Hey,

ich versuche mich seit einer Weile in Python, aber hier komme ich nicht weiter:

Code: Alles auswählen

import urllib.request, urllib.parse

username = input("username: ")
password = input("password: ")

dc = {"username":username, "password":password}
data = urllib.parse.urlencode(dc)

u = urllib.request.urlopen("https://login.rz.ruhr-uni-bochum.de/cgi-bin/laklogin", data)

li = u.readlines()
u.close()
for e in li:
    print(e)
Also möchte mich über einen Python Script (später mal automatisch) dort anmelden, es kommt aber immer folgende Fehlermeldung:
username: name
password: pw
Traceback (most recent call last):
File "/home/kim/Python/rubi.py", line 9, in <module>
u = urllib.request.urlopen("https://login.rz.ruhr-uni-bochum.de/cgi-bin/laklogin", data)
File "/usr/lib64/python3.2/urllib/request.py", line 138, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib64/python3.2/urllib/request.py", line 364, in open
req = meth(req)
File "/usr/lib64/python3.2/urllib/request.py", line 1052, in do_request_
raise TypeError("POST data should be bytes"
TypeError: POST data should be bytes or an iterable of bytes. It cannot be str.
Also so wie mir das jetzt erscheint ist urllib.parse.urlencode wohl hier nicht die richtige Wahl, oder ich muss noch etwas dazwischen schieben, aber was?

Vielen Dank

Re: urlencode für POST "data should be bytes"

Verfasst: Dienstag 4. Oktober 2011, 08:51
von lunar
Die Fehlermeldung ist doch eindeutig: Du musst eben "bytes" anstelle von "str" an "urlopen()" übergeben. Dazu musst Du "data" eben entsprechend umwandeln.

Re: urlencode für POST "data should be bytes"

Verfasst: Dienstag 4. Oktober 2011, 10:53
von chefhose
ok war nicht so schwer :)
Danke!