- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想用 hyper 下载大文件 (500mb),并在下载失败时继续下载。
hyper 有什么方法可以为接收到的每个数据 block 运行一些函数吗? send()
方法返回 Result<Response>
, 但我在 Response 上找不到任何方法返回 block 上的迭代器。理想情况下,我能够执行以下操作:
client.get(&url.to_string())
.send()
.map(|mut res| {
let mut chunk = String::new();
// write this chunk to disk
});
这可能吗,还是会 map
仅在 hyper 下载整个文件后调用?
最佳答案
Is there any way with hyper to run some function for each chunk of data received?
海珀的 Response
工具 Read
.这意味着 Response
是一个流,您可以从中读取任意数据 block ,就像您通常对流所做的那样。
对于它的值(value),这是我用来从 ICECat 下载大文件的一段代码。我正在使用 Read
界面,以便在终端中显示下载进度。
这里的变量response
是Hyper的Response
的一个实例。
{
let mut file = try_s!(fs::File::create(&tmp_path));
let mut deflate = try_s!(GzDecoder::new(response));
let mut buf = [0; 128 * 1024];
let mut written = 0;
loop {
status_line! ("icecat_fetch] " (url) ": " (written / 1024 / 1024) " MiB.");
let len = match deflate.read(&mut buf) {
Ok(0) => break, // EOF.
Ok(len) => len,
Err(ref err) if err.kind() == io::ErrorKind::Interrupted => continue,
Err(err) => return ERR!("{}: Download failed: {}", url, err),
};
try_s!(file.write_all(&buf[..len]));
written += len;
}
}
try_s!(fs::rename(tmp_path, target_path));
status_line_clear();
I want to download large files (500mb) with hyper, and be able to resume if the download fails.
这通常使用 HTTP“Range” header 实现(参见 RFC 7233)。
并非所有服务器都支持“Range” header 。我见过很多带有自定义 HTTP 堆栈但没有适当的“Range”支持,或者由于某种原因禁用了“Range” header 的服务器。因此,跳过 Hyper 的 Response
block 可能是必要的回退。
但是如果您想加快速度并节省流量,那么恢复停止下载的主要方法应该是使用“Range” header 。
附言对于 Hyper 0.12,Hyper 返回的响应主体是 Stream
并为接收到的每个数据 block 运行一些函数,我们可以使用 for_each
流组合器:
extern crate futures;
extern crate futures_cpupool;
extern crate hyper; // 0.12
extern crate hyper_rustls;
use futures::Future;
use futures_cpupool::CpuPool;
use hyper::rt::Stream;
use hyper::{Body, Client, Request};
use hyper_rustls::HttpsConnector;
use std::thread;
use std::time::Duration;
fn main() {
let url = "https://steemitimages.com/DQmYWcEumaw1ajSge5PcGpgPpXydTkTcqe1daF4Ro3sRLDi/IMG_20130103_103123.jpg";
// In real life we'd want an asynchronous reactor, such as the tokio_core, but for a short example the `CpuPool` should do.
let pool = CpuPool::new(1);
let https = HttpsConnector::new(1);
let client = Client::builder().executor(pool.clone()).build(https);
// `unwrap` is used because there are different ways (and/or libraries) to handle the errors and you should pick one yourself.
// Also to keep this example simple.
let req = Request::builder().uri(url).body(Body::empty()).unwrap();
let fut = client.request(req);
// Rebinding (shadowing) the `fut` variable allows us (in smart IDEs) to more easily examine the gradual weaving of the types.
let fut = fut.then(move |res| {
let res = res.unwrap();
println!("Status: {:?}.", res.status());
let body = res.into_body();
// `for_each` returns a `Future` that we must embed into our chain of futures in order to execute it.
body.for_each(move |chunk| {println!("Got a chunk of {} bytes.", chunk.len()); Ok(())})
});
// Handle the errors: we need error-free futures for `spawn`.
let fut = fut.then(move |r| -> Result<(), ()> {r.unwrap(); Ok(())});
// Spawning the future onto a runtime starts executing it in background.
// If not spawned onto a runtime the future will be executed in `wait`.
//
// Note that we should keep the future around.
// To save resources most implementations would *cancel* the dropped futures.
let _fut = pool.spawn(fut);
thread::sleep (Duration::from_secs (1)); // or `_fut.wait()`.
}
关于rust - 如何使用 hyper 下载大文件并在出错时恢复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41444297/
我第一次使用Hyper-V(运行Windows 8)。当我创建一个新的外部虚拟网络交换机时,它将虚拟机的网络适配器与WiFi桥接-出于某种原因,它会中断WiFi的Internet连接。为什么,我可能做
我有一个 vhd 文件,我需要在其上启用网络和远程连接。 vhd上安装了Windows Server 2008 R2和SharePoint开发平台、VS 2010... 我的笔记本电脑运行 Windo
我今天在我的 HP Windows 10 Pro 笔记本电脑上启用了 Hyper-V,它有第二台显示器,配置为扩展显示器(笔记本电脑是 #1,外接显示器是 #2)。重新启动计算机后,我注意到我的显示器
有谁知道为什么我在 hyper-v 管理器中看不到 HoloLens 2 模拟器作为虚拟机?据我所知,它是基于hyper-v的。我这样做是为了将默认虚拟交换机更改为外部虚拟交换机(也是虚拟交换机),以
Windows 手机模拟器需要 Hyper-V 才能运行,但 Android 模拟器又需要 Intel 硬件加速管理器 (HAXM),而后者无法兼容 Hyper-V。 有没有办法保持 Hyper-V
我在 Iron 处理程序中发出客户端请求。如何重用 Tokio 的 Core 和 Hyper 的 Client?我正在使用 hyper 0.11.0 和 tokio-core 0.1。 fn get_
我对此非常非常困惑。我有一台 Sony Vaio I7 笔记本电脑(bios 中有 Hyper-V 设置,所以我很容易启用 Hyper-V) 我想开发 Windows Phone 8。我已阅读 thi
我想使用 Hyper 来制作 HTTP 请求。调用 Client::get 工作正常,但其他方法,如 Client::post 和 Client::head 会导致编译错误。 extern crate
我们有一台大型服务器,上面运行着 Hyper-V 和一些虚拟机。我们的产品在虚拟机中的 Oracle DB 上运行时存在一些奇怪的问题,因此我们希望将 Oracle 直接安装到物理服务器上。我们可以限
如果你安装了win10系统,想做k8s集群,win10自带的hyper-v也是一个很适合的虚拟机环境。在上一节中,我们安装了两台虚拟机,这一节,都安装上docker。 master: 192.16
下面的教程在win2008与win2012 都可以 以上就完成了hyper-v虚拟机的创建工作。
第一步:Hyper-V创建虚拟交换机 第二步:虚拟机添加旧版网络适配器 好了,试下看可以上网了
Windows10安装Hyper-V后没有自动连接到本地计算机,手工连接失败,提示: 引用内容 尝试连接到服务器”DESKTOP-6P9L2HB”时出错。请检查虚拟机管理服务是否正在运行以及是否
我正在用 Hyper 编写一个“hello world”HTTP 服务器,但是我找不到 Server和 rt尝试导入模块时。 调用时cargo run ,然后我看到此错误消息: 26 | le
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 5年前关闭。 Improve this qu
我正在创建一个PowerShell脚本以作为计划任务运行。目的是在每个星期一在我们的每个虚拟机上创建一个快照。我为每个VM创建了一个简短的脚本。与本地Hyper-V服务器建立远程 session 后,
我有一个带有 2 个 NIC 但只有一个 IP 地址的 Windows Server 2008 R2,我已经安装了 Hyper-V。在 Hyper-V 内部,我创建了 2 个虚拟机。 我设置了一个外部
我正在尝试在 Hyper Web 服务器中创建一个计数器来计算它收到的请求数。我正在使用 Arc>坚持下去。但是,我一直无法找出 move 的正确组合。和 .clone()以满足闭包的类型。下面是一些
我遇到了一个编译错误,我对 master 中的示例稍作修改后不太理解Hyper 的分支。给定以下代码: extern crate futures; extern crate hyper; use fu
我在运行添加了对 HTTP/2 协议(protocol)支持的 Python hyper 库时遇到问题。 在我的 Fedora 机器上,我使用 pip 和 pip3 安装它,以便将它与 Python
我是一名优秀的程序员,十分优秀!