目录
1.Remote end closed connection without response
2.ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。
问题描述:
最近在python学习的时候,按照老师教的方法敲的代码(因为是过去的视频),已经不管用了。
看了网上的 原因,
1.频繁访问导致远程怀疑是恶意攻击
2.User-Agent
被网站记录不允许访问
但是我是第一次运行代码,并且我的User-Agent也是自己的,按理说不应该会出现上面两个问题
感觉问题应该是出在最后面的response里面或者发起请求那三段的某个出现的问题
我有用try/except监控代码错误,报的第一个错误
过程:
1.Remote end closed connection without response
try:
# url="https://www.douban.com"
url = "http://www.baidu.com"
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64;x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36"
}
data = bytes(urllib.parse.urlencode({"name":"epic"}),encoding="utf-8")
req = urllib.request.Request(url=url,data=data,headers=headers,method="post")
#发起请求
response = urllib.request.urlopen(req,timeout=10) #我们封装了一个对象,req,我们将req发给网站,让网站人为我们是浏览器。这里仅向的操作是对浏览器发起请求
print(response.read().decode("utf-8"))
response.close()
except Exception as result:
print(result)