본문 바로가기

웹(web)/백엔드-python7

parse_qs(POST data parsing) 클라이언트의 요청이 POST 방식이면 data는 entity body에 담겨서 온다. 따라서 별도의 작업을 해야 data를 사용할 수 있다. POST data parsing 예제 magic=mystery&secret=spooky 의 데이터를 서버에게 보냈다고 하자. 1. content lenght를 파악한다. length = int(self.headers.get('Content-length', 0)) self.headers.get은 header field에 있는 값들을 가져올 때 쓰인다. 헤더 정보 중에 키가 'Content-length'인 값을 가져오고 'Content-length'가 없을 경우에 0을 리턴하겠다는 뜻이다. 2. entity body에서 데이터 디코딩하여 "magic=mystery&se.. 2020. 7. 1.
EchoServer HelloServer에서 entity body를 쓰는 부분이 self.wfile.write() 부분인 것을 설명했다. 브라우저에 localhost:8000/helloPath! 를 치면 화면에 helloPath! 를 보여주려면 어떻게 할까? self.wfile.write(self.path[1:].encode())로 바꾸면 된다. 브라우저에 localhost:8000/helloPath! 라고 치면 우리가 작성한 코드에서 self.path에는 "/helloPath!"가 된다. 요청 -> 서버(요청한 url에서 path는 self.path로 사용가능) -> 응답 더 궁금하면 BaseHTTPRequestHandler에 대한 정보를 찾아보면 좋을 것이다. 2020. 7. 1.
HelloServer 브라우저에서 localhost:8000를 입력하면 Hello, HTTP!를 화면에 보여주는 예를 살펴보자. HelloServer.py 코드 from http.server import HTTPServer, BaseHTTPRequestHandler class HelloHandler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-type', 'text/plain; charset=utf-8') self.end_headers() self.wfile.write("Hello, HTTP!\n".encode()) if __name__ == '__main__': server_address = ('', 8.. 2020. 7. 1.