python中的“requests”没有完全下载文件

2024-03-28 17:33:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python的请求库来下载一个大小约为40mb的文件。但是我的代码只得到14mb大小的文件。它没有显示任何错误(尽管在下载文件之前很少有警告)。在

这是我的代码:

import requests
file_url = "https://file_url.tar"

user='username'
passw='password'
r = requests.get(file_url,auth=(user,passw),verify=False, stream = True)
with open("c.tar","wb") as code:
   for chunk in r.iter_content(chunk_size=1024):
       if chunk:
         code.write(chunk)

我也试过不使用stream=True。但这也不管用。在

当我把这个网址放在浏览器里,我会得到40 mb的完整文件。在

我在其他机器上尝试了这个脚本,它在那里运行得很好(我在这里也得到了那些警告)。在

以下是我收到的警告:

SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning. SNIMissingWarning

InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning. InsecurePlatformWarning

InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html InsecureRequestWarning)

但我不认为这有什么问题,因为如果我在其他系统上运行这个脚本,我会收到这些警告,但脚本运行得很好。在


Tags: 文件tohttpsorg脚本url警告is
1条回答
网友
1楼 · 发布于 2024-03-28 17:33:50

我正在使用urllib而不是请求

import urllib
url = "http://62.138.7.10/downloads/Bh2g2m.Bh2g.06.DR.M0viesC0unter.mp4?st=6MVZyTUL7X22v7ILOtB2XA&e=1502823147"
file_name = 'trial_video.mp4'
response = urllib.urlopen(url)
with open(file_name,'wb') as f:
    f.write(response.read())

希望这对您有帮助

相关问题 更多 >