python - PUT request blocking too long with simple custom HTTP client and server -


i have implemented simple http server , client. latter issues put request using requests library, sending arbitrary json , exits.

when start server, , run client, both server , client block. server appears not have gone through entire handler function yet.

this on server side:

$ python3 server.py put / http/1.1 

that is, after printing request line, content json string not printed. @ point both client , server block reason.

interestingly, when trigger keyboardinterrupt client, server proceeds:

$ python3 server.py put / http/1.1 b'{"content": "hello world"}' 127.0.0.1 - - [25/feb/2016 11:52:54] "put / http/1.1" 200 - 

my questions:

  • why necessary kill client let server proceed?
  • am using of these components wrong way?
  • how can make client , server operate (nearly) instantaneously?

this code of http server. handles put requests. prints request line , content data , responds using success code client:

import http.server  class printputrequesthandler(http.server.basehttprequesthandler):     def do_put(self):         print(self.requestline)         print(self.rfile.read())         self.send_response(200)         self.end_headers()  server_address = ('', 8000) httpd = http.server.httpserver(server_address, printhttprequesthandler) httpd.serve_forever() 

this http client. intended connect server, write request , return possible (but doesn't):

import requests  server_address = "http://127.1:8000" data = '{"content": "hello world"}' requests.put(server_address, data, headers={"content-type": "application/json"}) 

this how run after server has started (no output observable):

python client.py 

the server blocks both , client on line:

print(self.rfile.read()) 

that happens because didn't specify amount of data read server reads input stream until closed. , in case input stream closed once kill client.

remember server doesn't know priori when streaming of data ends because may want send data chunk chunk (for example when send big files).

the size of request should passed in content-length header should do:

length = int(self.headers['content-length']) print(self.rfile.read(length)) 

that's assuming length small enough fit in memory (and in case is).


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -