Enrico's Ricardo
Enrico's Ricardo

Reputation: 59

Python HTTPServer - get HTTP body

I wrote a HTTP server using python, but I do not know how to get the HTTP body. what should I do to get the HTTP body?

here is my code:

from http.server import HTTPServer,BaseHTTPRequestHandler

class MyHTTPHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        print("connect from ",self.client_address)
        print(self.headers)
        length = self.headers['Content-Length']
        print(length)

addr = ('',21567)
server = HTTPServer(addr,MyHTTPHandler)
server.serve_forever()

Upvotes: 3

Views: 5349

Answers (1)

LiGhTx117
LiGhTx117

Reputation: 218

Having a request body in a GET request is not a good practice, as its discussed here: HTTP GET with request body

Instead, you can change your method to POST and check there the BaseHTTPRequestHandler documentation: https://docs.python.org/2/library/basehttpserver.html

Especially this part:

rfile

Contains an input stream, positioned at the start of the optional input data.

Upvotes: 2

Related Questions