简体   繁体   English

函数cgi.FieldStorage冻结Python HTTPS服务器

[英]Function cgi.FieldStorage freezes Python HTTPS server

I am writing a Python HTTPS server where clients upload files (binaries between 1 to 10 MB) into it. 我正在编写一个Python HTTPS服务器,客户端将文件(1到10 MB之间的二进制文件)上传到其中。 I use a HTTPServer with a custom BaseHTTPRequestHandler . 我将HTTPServer与自定义BaseHTTPRequestHandler一起使用 The request are processed by this following code: 该请求由以下代码处理:

ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
if ctype == "multipart/form-data":
    fs = cgi.FieldStorage(fp = self.rfile, \
        headers = self.headers, \
        environ = {'REQUEST_METHOD':'POST'})

The client is written in C++ with libcurl . 客户端使用libcurl用C ++编写。 The request is send by this following code: 该请求通过以下代码发送:

std::string url = getURLServer("senddata");
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
std::string szPostFields = "id=" + std::to_string(ID);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, szPostFields.c_str());

struct curl_httppost *post = NULL;
struct curl_httppost *last = NULL;
curl_formadd(&post, &last,
    CURLFORM_COPYNAME, "logfile",
    CURLFORM_FILECONTENT, ".\\log.bin",
    CURLFORM_END);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);

curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, "PEM");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);

res = curl_easy_perform(curl);

The issue is the server freezes at cgi.FieldStorage(...) . 问题是服务器在cgi.FieldStorage(...)处冻结。 It doesn't freeze if I don't upload a file. 如果我不上传文件,它不会冻结。

My mistake ! 我的错 ! I was reading first the input with this code in order to get parameters: 我首先阅读此代码的输入以获取参数:

length = int(self.headers.getheader('content-length'))
field_data = self.rfile.read(length)
fields = urlparse.parse_qs(field_data)

And then doing cgi.FieldStorage to get the actual uploaded file. 然后执行cgi.FieldStorage以获取实际的上传文件。 Actually, I suppose that the initial read consumes the socket input and what I took for a freeze it is just my server waiting for new data. 实际上,我认为初始读取消耗了套接字输入,而我冻结的只是我的服务器在等待新数据。

Finally, I just removed this 3 lines and process all data with cgi.FieldStorage solely. 最后,我只删除了这3行,仅使用cgi.FieldStorage处理所有数据。

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

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