gpt4 book ai didi

C 程序在从 Flask 服务器获得响应时挂起

转载 作者:可可西里 更新时间:2023-11-01 17:34:55 26 4
gpt4 key购买 nike

编辑 经过更多阅读,破损的管道错误意味着我终止了连接。但问题依然存在,为什么read请求挂了?

我对此很陌生,所以请多多包涵。

我在本地主机上运行的 flask 中有一个微型 web 应用程序,如下所示:

import stuff
app = Flask(__name__)
...do some stuff
@app.route("/")
def foo():

resp_OK = Response(response="", status=200)
resp_NO = Response(response="", status=500)

...do some stuff

if some stuff:
return resp_NO
else:
return resp_OK

if __name__== '__main__':
app.run()

如果我只输入 localhost:5000/?param1=therightparamts&param2=tomakeitwork,一切正常,相应的命令行响应是 200 或 500。

现在,我正尝试从 C 内部获取此响应,我从 Jeremy Jeremiah 的响应中借用的代码 here .据我所知,它打开套接字并发送请求,然后无法读取响应。我只想知道 webapp 返回的是 200 还是 500。这是相关的 C 代码:

char response[4096];
int total, received, bytes;
...
sockfd = socket(AF_INET, SOCK_STREAM, 0);
...
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
...
do {
bytes = write(sockfd,&(message.c[sent]),total-sent);
...}while(...);
...
memset(response,0,sizeof(response));
total = sizeof(response)-1;
received = 0;
do {
bytes = read(sockfd,response+received,total-received);
printf("Read %i bytes.\n", received); fflush(stdout);
if (bytes < 0)
error("ERROR reading response from socket");
if (bytes == 0)
break;
received+=bytes;
} while (received < total);

它永远不会在 read(sockfd...) 之后到达 printf。如果我在 C 程序挂起时终止它,控制台输出是

127.0.0.1 - - [27/Dec/2015 14:25:33] "GET /?file=foo&signature=46b4ec586117154dacd49d664e5d63fdc88efb51 HTTP/1.1" 500 -
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 2363)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 657, in __init__
self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 716, in finish
self.wfile.close()
File "/usr/lib/python2.7/socket.py", line 283, in close
self.flush()
File "/usr/lib/python2.7/socket.py", line 307, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------

如果我在C程序挂起时键盘中断webapp,响应如下:

Exception happened during processing of request from ('127.0.0.1', 3760)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 655, in __init__
self.handle()
File "/home/gmoss/.local/lib/python2.7/site-packages/werkzeug/serving.py", line 217, in handle
rv = BaseHTTPRequestHandler.handle(self)
File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
self.handle_one_request()
File "/home/gmoss/.local/lib/python2.7/site-packages/werkzeug/serving.py", line 251, in handle_one_request
elif self.parse_request():
File "/usr/lib/python2.7/BaseHTTPServer.py", line 291, in parse_request
self.headers = self.MessageClass(self.rfile, 0)
File "/usr/lib/python2.7/mimetools.py", line 25, in __init__
rfc822.Message.__init__(self, fp, seekable)
File "/usr/lib/python2.7/rfc822.py", line 108, in __init__
self.readheaders()
File "/usr/lib/python2.7/rfc822.py", line 155, in readheaders
line = self.fp.readline()
File "/usr/lib/python2.7/socket.py", line 451, in readline
data = self._sock.recv(self._rbufsize)
KeyboardInterrupt
----------------------------------------

我查看了 SO 上的其他一些答案,但不太了解如何将这些答案应用到我的情况中(正如我所说,这是新手)。将不胜感激任何指向正确方向的指示。

最佳答案

通过放弃上述方法并改用 cURL 使其工作。 (stringnewString 等是我自己的自定义字符串 utils)。

int curlRequest(int argc, string *argv)
{
CURL *curl;
CURLcode res;
string url;
int i;
long http_code;

url=newString("http://localhost:5000",0);
/* fill in the parameters */
url=stringCat(url, newString("/?",0));
for(i=0; i<argc; i++){
url = stringCat(url, argv[i]);
if(i+1<argc) url = stringCat(url, newString("&",0));
}
//url = stringCat(url, newString(" HTTP/1.1\r\n",0));


curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c);

/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);

/* always cleanup */
curl_easy_cleanup(curl);
}
return http_code;
}

关于C 程序在从 Flask 服务器获得响应时挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34485564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com