I am trying to login to a website using urllib in Python using the following code:
import urllib.parse
import urllib.request
headers = {"Content-type": "application/x-www-form-urlencoded"}
payload = urllib.parse.urlencode({"username": "USERNAME-HERE",
"password": "PASSWORD-HERE",
"redirect": "index.php",
"sid": "",
"login": "Login"}).encode("utf-8")
request = urllib.request.Request("https://osu.ppy.sh/forum/ucp.php?mode=login", payload, headers)
response = urllib.request.urlopen(request)
data = response.read()
# print the HTML after the request
print(bytes(str(data), "utf-8").decode("unicode_escape"))
I know that a common suggestion is to just use the Requests library, and I have tried this, but I specifically want to know how to do this without using Requests.
The behavior I am looking for can be replicated with the following code that successfully logs in to the site using http.client:
import urllib.parse
import http.client
headers = {"Content-type": "application/x-www-form-urlencoded"}
payload = urllib.parse.urlencode({"username": "USERNAME-HERE",
"password": "PASSWORD-HERE",
"redirect": "index.php",
"sid": "",
"login": "Login"})
conn = http.client.HTTPSConnection("osu.ppy.sh")
conn.request("POST", "/forum/ucp.php?mode=login", payload, headers)
response = conn.getresponse()
data = response.read()
# print the HTML after the request
print(bytes(str(data), "utf-8").decode("unicode_escape"))
It seems to me that the urllib code is not "delivering" the payload, while the http.client code is.
I seem to be able to "deliver" the payload, as providing a faulty password and username warrants a response from the server, but providing a correct username and password seems to have no effect.
Any insights? Am I overlooking something?