简体   繁体   中英

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. I use a HTTPServer with a custom 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 . 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(...) . 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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