3

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()
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

2

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.

Community
  • 1
  • 1
LiGhTx117
  • 218
  • 2
  • 8