0

Python BaseHTTPRequestHandler имеет проблему с формами, отправленными через почту!Почему do_GET намного быстрее, чем do_POST

Я видел других людей задают тот же вопрос (Why GET method is faster than POST?), но разница во времени в моем случае это слишком много (1 секунда)

Python сервер:

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer 
import datetime 


def get_ms_since_start(start=False): 
    global start_ms 
    cur_time = datetime.datetime.now() 
    # I made sure to stay within hour boundaries while making requests 
    ms = cur_time.minute*60000 + cur_time.second*1000 + int(cur_time.microsecond/1000) 
    if start: 
     start_ms = ms 
     return 0 
    else: 
     return ms - start_ms 

class MyServer(BaseHTTPRequestHandler, object): 
    def do_GET(self): 
     print "Start get method at %d ms" % get_ms_since_start(True) 
     field_data = self.path 
     self.send_response(200) 
     self.end_headers() 
     self.wfile.write(str(field_data)) 
     print "Sent response at %d ms" % get_ms_since_start() 
     return 

    def do_POST(self): 
     print "Start post method at %d ms" % get_ms_since_start(True) 
     length = int(self.headers.getheader('content-length')) 
     print "Length to read is %d at %d ms" % (length, get_ms_since_start()) 
     field_data = self.rfile.read(length) 
     print "Reading rfile completed at %d ms" % get_ms_since_start() 
     self.send_response(200) 
     self.end_headers() 
     self.wfile.write(str(field_data)) 
     print "Sent response at %d ms" % get_ms_since_start() 
     return 


if __name__ == '__main__': 
    server = HTTPServer(('0.0.0.0', 8082), MyServer) 
    print 'Starting server, use <Ctrl-C> to stop' 
    server.serve_forever() 

Get запрос с питона сервер очень быстро

time curl -i http://0.0.0.0:8082\?one\=1 

печатает

HTTP/1.0 200 OK 
Server: BaseHTTP/0.3 Python/2.7.6 
Date: Sun, 18 Sep 2016 07:13:47 GMT 

/?one=1curl http://0.0.0.0:8082\?one\=1 0.00s user 0.00s system 45% cpu 0.012 total 

и на стороне сервера:

Start get method at 0 ms 
127.0.0.1 - - [18/Sep/2016 00:26:30] "GET /?one=1 HTTP/1.1" 200 - 
Sent response at 0 ms 

мгновенная!

запрос сообщения при отправке формы на питон сервера очень медленно

time curl http://0.0.0.0:8082 -F one=1 

печатает

--------------------------2b10061ae9d79733 
Content-Disposition: form-data; name="one" 

1 
--------------------------2b10061ae9d79733-- 
curl http://0.0.0.0:8082 -F one=1 0.00s user 0.00s system 0% cpu 1.015 total 

и на стороне сервера:

Start post method at 0 ms 
Length to read is 139 at 0 ms 
Reading rfile completed at 1002 ms 
127.0.0.1 - - [18/Sep/2016 00:27:16] "POST/HTTP/1.1" 200 - 
Sent response at 1002 ms 

В частности, сам. rfile.read (length) занимает 1 с econd для очень маленьких форм данных

Сообщения запроса при передаче данных (не образует) на питон сервер очень быстро

time curl -i http://0.0.0.0:8082 -d one=1 

печатает

HTTP/1.0 200 OK 
Server: BaseHTTP/0.3 Python/2.7.6 
Date: Sun, 18 Sep 2016 09:09:25 GMT 

one=1curl -i http://0.0.0.0:8082 -d one=1 0.00s user 0.00s system 32% cpu 0.022 total 

и на стороне сервера:

Start post method at 0 
Length to read is 5 at 0 
Reading rfile completed at 0 
127.0.0.1 - - [18/Sep/2016 02:10:18] "POST/HTTP/1.1" 200 - 
Sent response at 0 

node.js сервер:

var http = require('http'); 
var qs = require('querystring'); 

http.createServer(function(req, res) { 
    if (req.method == 'POST') { 
     whole = '' 
     req.on('data', function(chunk) { 
      whole += chunk.toString() 
     }) 

     req.on('end', function() { 
      console.log(whole) 
      res.writeHead(200, 'OK', {'Content-Type': 'text/html'}) 
      res.end('Data received.') 
     }) 
    } 
}).listen(8082) 

Сообщение запроса при отправке формы на сервер Node.js очень быстро

time curl -i http://0.0.0.0:8082 -F one=1 

печатает:

HTTP/1.1 100 Continue 

HTTP/1.1 200 OK 
Content-Type: text/html 
Date: Sun, 18 Sep 2016 10:31:38 GMT 
Connection: keep-alive 
Transfer-Encoding: chunked 

Data received.curl -i http://0.0.0.0:8082 -F one=1 0.00s user 0.00s system 42% cpu 0.013 total 
+0

Если вы укажете небольшой размер, например 5 для чтения() на self.rfile (второй аргумент), ускорится ли оно? – datasedai

ответ

2

Я думаю, что это ответьте на вашу проблему: libcurl delays for 1 second before uploading data, command-line curl does not

libcurl отправляет заголовок Expect 100-Continue и ждет 1 секунду для ответа перед отправкой данных формы (в случае команды -F).

В случае -d, по какой-либо причине он не отправляет заголовок 100-Continue.

+0

Использование 'self.send_response (100); self.end_headers() ' в начале моего метода do_POST решила проблему. Большое спасибо! – user34812

Смежные вопросы