python3 http post get 收发数据

python3 http post get 收发数据

from http.server import HTTPServer, BaseHTTPRequestHandler
import json
import urllib


data = {'result':'this is test'}

host = ('localhost',12345)

class Resquest(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type', 'application/json')
        self.end_headers()
        self.wfile.write(json.dumps(data).encode())
        print("do_GET")

    def do_POST(self):
        length = int(self.headers['Content-Length'])
        post_data = json.loads(self.rfile.read(length))
        print(post_data)

if __name__ == '__main__':
    server = HTTPServer(host, Resquest)
    print("Starting Server....")
    server.serve_forever()

你可能感兴趣的:(python)