Login to the site using urllib instead of http.client

I am trying to login to a site 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 the general suggestion is to just use the Requests library, and I've tried that, but I specifically want to know how to do this without using requests.

The behavior I'm looking for can be replicated with the following code, which successfully registers with the site with 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 code urllib

does not "deliver" the payload, but the code http.client

.

I seem to be able to "deliver" the payload since providing the wrong password and username requires a response from the server, but providing the correct username and password seems ineffective.

Any ideas? Am I missing something?

+3


source to share


1 answer


Add a cookie doll and take out the headers as they are not needed with urllib

:



import http.cookiejar
import urllib.parse
import urllib.request

jar = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(jar))

payload = urllib.parse.urlencode({"username": "USERNAME-HERE",
                                  "password": "PASSWORD-HERE",
                                  "redirect": "index.php",
                                  "sid": "",
                                  "login": "Login"}).encode("utf-8")
response = opener.open("https://osu.ppy.sh/forum/ucp.php?mode=login", payload)
data = response.read()

# print the HTML after the request
print(bytes(str(data), "utf-8").decode("unicode_escape"))

      

+2


source







All Articles