ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。
import socket
socket请求一张图片
http://desk.zol.com.cn/bizhi/8341_103503_2.html
socket.setdefaulttimeout(20) # 设置socket层的超时时间为20秒
b = socket.socket()
b.connect((‘desk.zol.com.cn’, 80))
路径
req_line = ‘GET / it/u=2426073820,570289973&fm=214&gp=0.jpg HTTP/1.1\r\n’
Sec-Fetch-Mode: no-cors\r\n
req_header = ‘Host: desk.zol.com.cn\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36\r\nReferer: http://desk.zol.com.cn/\r\nSec-Fetch-Mode: no-cors\r\n’
empty_line = ‘\r\n’
req_content = ‘’
b.send((req_line + req_header + empty_line + req_content).encode())
complete_content = b’’
res_content = b.recv(4096)
while req_content:
# 文件分多次传递
complete_content += req_content
# 如果为空就会跳出循环
req_content = b.recv(4096)
with open(’./test2.jpg’, ‘wb’) as f:
f.write(complete_content)
f.close()