Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用urllib而不是http.client登录网站_Python_Python 3.x_Http_Post_Urllib - Fatal编程技术网

Python 使用urllib而不是http.client登录网站

Python 使用urllib而不是http.client登录网站,python,python-3.x,http,post,urllib,Python,Python 3.x,Http,Post,Urllib,我正在尝试使用Python中的urllib登录到一个网站,使用以下代码: import urllib.parse import urllib.request headers = {"Content-type": "application/x-www-form-urlencoded"} payload = urllib.parse.urlencode({"username": "USERNAME-HERE", "password"

我正在尝试使用Python中的
urllib
登录到一个网站,使用以下代码:

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"))
我知道一个常见的建议是只使用Requests库,我已经尝试过了,但我特别想知道如何在不使用Requests的情况下做到这一点

我正在寻找的行为可以通过以下代码复制,这些代码使用http.client
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"))
在我看来,
urllib
代码并没有“交付”有效负载,而
http.client
代码是

我似乎能够“交付”负载,因为提供错误的密码和用户名可以保证服务器的响应,但提供正确的用户名和密码似乎没有效果


有什么见解吗?我忽略了什么吗?

添加一个cookie jar并取出标题,因为它们不需要使用
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"))

我尝试运行您的代码,得到了预期结果(您指定的密码不正确…)。这意味着有效载荷已交付。如果你填写了正确的用户名/密码,你会得到什么样的回应?@Qeek我也看到了你提到的行为,但是当给出了正确的用户名和密码时,网站仍然表现得好像我没有登录(“欢迎,来宾!”,而不是“欢迎,!”)。可能在初始有效负载交付后登录没有持续存在?我需要用饼干吗?