请求返回504,但urllib返回200,为什么?

时间:2020-11-04 10:50:48

标签: python ssl urllib

我想请求URL并尝试使用Requests模块请求,但返回504,或者使用SSL.usllib。protocol_TLSV1_2返回正常。这是我的代码:

headers = {
    'Host': 'mall.xsyxsc.com',
    'charset': 'utf-8',
    'User-Agent': 'Mozilla/5.0 (Linux; Android 9; Redmi Note 8 Pro Build/PPR1.180610.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.62 XWEB/2693 MMWEBSDK/200901 Mobile Safari/537.36 MMWEBID/297 MicroMessenger/7.0.19.1760(0x27001355) Process/appbrand0 WeChat/arm64 NetType/WIFI Language/zh_CN ABI/arm64',
}

data = 'storeId=66880000088180&areaId=106&openBrandHouse=OPEN'
url = 'https://mall.xsyxsc.com/user/product/indexWindows'

requests_resp = requests.post(url, data=data, headers=headers)
print("this requests error resp: ", requests_resp)

context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
response = urllib.request.urlopen(
    url=urllib.request.Request(url=url, headers=headers),
    data=data.encode(),
    context=context,
)
response = response.read().decode()
print("this urllib suc resp:", response)

我试图搜索相关问题,例如:

  1. Python Requests requests.exceptions.SSLError: [Errno 8] _ssl.c:504: EOF occurred in violation of protocol

  2. Forcing requests library to use TLSv1.1 or TLSv1.2 in Python

  3. https://github.com/psf/requests/issues/5599

但是它们不能正常工作。 我想知道为什么请求不起作用以及如何使它们起作用

谢谢!

0 个答案:

没有答案