简体   繁体   English

如果请求包含 Connection: keep-alive,我应该什么时候关闭套接字?

[英]When should I close the socket, if the request contains Connection: keep-alive?

When I am using this Python script to send a request to my server:当我使用这个 Python 脚本向我的服务器发送请求时:

import requests as r

url = "http://localhost:8070/"
response = r.get(url=url)

It sends the following request:它发送以下请求:

GET / HTTP/1.1
Host: localhost:8070
User-Agent: python-requests/2.27.1
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive

If I understand it correctly, Connection: keep-alive means that I should not close the client-socket, because the client could use it again.如果我理解正确的话, Connection: keep-alive意味着我不应该关闭客户端套接字,因为客户端可以再次使用它。

But if I do not close the client-socket, the Python script just gets stuck until I close the socket.但是如果我不关闭客户端套接字,Python 脚本就会卡住,直到我关闭套接字。 Is there another way to indicate that the request is complete so pythons requests understands it?是否有另一种方法表明请求已完成以便 pythons 请求理解它?

If I try to send the request to any other server, the script almost immediately finishes.如果我尝试将请求发送到任何其他服务器,脚本几乎会立即完成。 My guess would be to timeout the client after a few milliseconds eg by using select like this:我的猜测是在几毫秒后使客户端超时,例如使用 select,如下所示:

struct timeval timeout;

timeout.tv_sec = 0;
timeout.tv_usec = 1000;
select_ret = select(this->_maxfds + 1, &this->_readfds, &this->_writefds, NULL, &timeout);

And now I would just close the client-socket after select returns 0:现在我将在 select 返回 0 后关闭客户端套接字:

if (select_ret == 0) {
   close(client_socket);
}

Is that a valid approach, or am I missing something?这是一种有效的方法,还是我遗漏了什么?

I am sending the response like this:我正在发送这样的回复:

char *response = "HTTP/1.1 200 Ok\r\n\r\n";
send(this->_client_socket, response, strlen(response), 0) 

BUT this does not terminate the python script.但这不会终止 python 脚本。 The python script still hangs after I execute this line of code.执行这行代码后,python 脚本仍然挂起。 It only finishes when I close the socket on my side.只有当我关闭我这边的插座时它才会结束。

So how would I determine if I should close it or not?那么我如何确定是否应该关闭它呢? As I already said my approach was to use a timeout in case no data is getting written in to the socket from the client side.正如我已经说过的,我的方法是在没有数据从客户端写入套接字的情况下使用超时。

Your server's response is incomplete.您的服务器响应不完整。

Your understanding of Connection: keep-alive in the request is correct.您对请求中Connection: keep-alive的理解是正确的。 However, there is no Content-Length or Transfer-Encoding: chunked header present in your response, so the only way the client has to know when the response is finished is to wait for the socket connection to be closed on the server side.但是,您的响应中没有Content-LengthTransfer-Encoding: chunked chunked header ,因此客户端必须知道响应何时完成的唯一方法是等待服务器端关闭套接字连接。 Read the rules outlined in RFC 2616 Section 4.4 and RFC 7230 Section 3.3.3 of the HTTP 1.1 protocol spec.阅读 HTTP 1.1 协议规范的RFC 2616 第 4.4 节RFC 7230 第 3.3.3节中概述的规则。

Try something more like this instead:尝试更像这样的东西:

const char *response = "HTTP/1.1 200 Ok\r\nContent-Length: 0\r\n\r\n";
send(this->_client_socket, response, strlen(response), 0) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM