我想在Rust中编写简单的TCP/IP客户端,我需要打印出从服务器获得的缓冲区.如何将u8矢量转换String为打印?
我正在尝试使用actix_web来获取并显示网页的内容。HTTP 请求成功完成,我可以查看网页,但我想将正文读入String打印。
我尝试过let my_ip: String = response.body().into();,但收到一条错误消息
error[E0277]: the trait bound `std::string::String: std::convert::From<actix_web::httpmessage::MessageBody<actix_web::client::response::ClientResponse>>` is not satisfied
--> src/main.rs:16:53
|
16 | let my_ip: String = response.body().into();
| ^^^^ the trait `std::convert::From<actix_web::httpmessage::MessageBody<actix_web::client::response::ClientResponse>>` is not implemented for `std::string::String`
|
= help: the following implementations were found:
<std::string::String as std::convert::From<&'a str>>
<std::string::String as std::convert::From<std::borrow::Cow<'a, str>>>
<std::string::String as std::convert::From<std::boxed::Box<str>>>
<std::string::String as std::convert::From<trust_dns_proto::error::ProtoError>>
= note: required because of the requirements on the impl of `std::convert::Into<std::string::String>` for `actix_web::httpmessage::MessageBody<actix_web::client::response::ClientResponse>`
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所拥有的:
use …Run Code Online (Sandbox Code Playgroud) 作为一个非常简单的例子,我正在尝试编写一个简单回复的网络服务器
此页面已被要求$ N次
但是我在分享可变状态方面遇到了很多麻烦.这是我最好的尝试:
extern crate hyper;
use hyper::Server;
use hyper::server::Request;
use hyper::server::Response;
struct World {
count: i64,
}
impl World {
fn greet(&mut self, req: Request, res: Response) {
self.count += 1;
let str: String = format!("this page has been requested {} times", self.count);
res.send(str.as_bytes()).unwrap();
}
}
fn main() {
println!("Started..");
let mut w = World { count: 0 };
Server::http("127.0.0.1:3001").unwrap()
.handle(move |req: Request, res: Response| w.greet(req, res) ).unwrap();
}
Run Code Online (Sandbox Code Playgroud) 一个答案 如何阅读基于东京-超请求的整个身体?建议:
您可能希望对读取的字节数设置某种上限 [使用时
futures::Stream::concat2]
我怎样才能真正做到这一点?例如,这里有一些代码模拟向我的服务发送无限量数据的恶意用户:
extern crate futures; // 0.1.25
use futures::{prelude::*, stream};
fn some_bytes() -> impl Stream<Item = Vec<u8>, Error = ()> {
stream::repeat(b"0123456789ABCDEF".to_vec())
}
fn limited() -> impl Future<Item = Vec<u8>, Error = ()> {
some_bytes().concat2()
}
fn main() {
let v = limited().wait().unwrap();
println!("{}", v.len());
}
Run Code Online (Sandbox Code Playgroud)